Changeset 1196 for proto/Compiler


Ignore:
Timestamp:
Jun 18, 2011, 2:15:09 PM (8 years ago)
Author:
cameron
Message:

C syntax mode using macros

File:
1 edited

Legend:

Unmodified
Added
Removed
  • proto/Compiler/pablo.py

    r1195 r1196  
    277277    if carries == 0: return whileNode
    278278    carry_arglist = [self.carryvar, ast.Num(carry_base), ast.Num(carries)]
    279     local_carryvar = 'sub'+self.carryvar.id
     279    local_carryvar = 'subcarryQ'
    280280    inner_while = CarryIntro(local_carryvar, '', self.carryout).generic_xfrm(copy.deepcopy(whileNode))
    281281    self.generic_visit(whileNode)
     
    405405             node.func.id = name + ("_" if self.C_syntax else ".") + ("do_final_block" if self.xlate_type == "final" else "do_block")
    406406             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]
     407                     node.args = [ast.Name(lower1(name), ast.Load())] + node.args
    408408             if self.xlate_type == "final":
    409409                   node.args = node.args + [ast.Name("EOF_mask", ast.Load())]
     
    438438    return name[0].upper() + name[1:]
    439439
     440def escape_newlines(str):
     441  return str.replace('\n', '\\\n')
     442
    440443class Emitter():
    441444        def __init__(self, use_C_syntax):
     
    466469
    467470                if self.use_C_syntax:
    468                         return self.indent(icount) + "struct " + stream_function.type_name + "_data {" \
     471                        return self.indent(icount) + "struct " + stream_function.type_name + " {" \
    469472                               + "\n" + self.indent(icount) + carry_declaration \
    470473                               + "\n" + self.indent(icount) + "};\n" \
     
    486489        def do_block(self, parameters, declarations, initializations, statements, icount=0):
    487490                pfx = (lower1(self.type_name) + "_" if self.use_C_syntax else "")
     491                if self.use_C_syntax:
     492                        return "#define " + pfx + "do_block(" + parameters + ")\\\n do {" \
     493                        + "\\\n" + self.indent(icount) + escape_newlines(declarations) \
     494                        + "\\\n" + self.indent(icount) + escape_newlines(initializations) \
     495                        + "\\\n" + self.indent(icount) + escape_newlines(statements) \
     496                        + "\\\n" + self.indent(icount + 2) + "} while (0)"
    488497                return self.indent(icount) + "void " + pfx + "do_block(" + parameters + ") {" \
    489498                + "\n" + self.indent(icount) + declarations \
     
    492501                + "\n" + self.indent(icount + 2) + "}"
    493502
     503
     504
     505
    494506        def do_final_block(self, parameters, declarations, initializations, statements, icount=0):
    495507                pfx = (lower1(self.type_name) + "_" if self.use_C_syntax else "")
     508                if self.use_C_syntax:
     509                        return "#define " + pfx + "do_final_block(" + parameters + ")\\\n do {" \
     510                        + "\\\n" + self.indent(icount) + escape_newlines(declarations) \
     511                        + "\\\n" + self.indent(icount) + escape_newlines(initializations) \
     512                        + "\\\n" + self.indent(icount) + escape_newlines(statements) \
     513                        + "\\\n" + self.indent(icount + 2) + "} while (0)"
    496514                return self.indent(icount) + "void " + pfx + "do_final_block(" + parameters + ") {" \
    497515                + "\n" + self.indent(icount) + declarations \
     
    502520        def do_segment(self, parameters, do_block_call_args, icount=0):
    503521                pfx = (lower1(self.type_name) + "_" if self.use_C_syntax else "")
     522                if self.use_C_syntax:
     523                        return "#define " + pfx + "do_segment(" + parameters + ")\\\n do {" \
     524                        + "\\\n" + self.indent(icount) + "  int i;" \
     525                        + "\\\n" + self.indent(icount) + "  for (i = 0; i < SEGMENT_BLOCKS; i++)" \
     526                        + "\\\n" + self.indent(icount) + "    " + pfx + "do_block(" + do_block_call_args + ");" \
     527                        + "\\\n" + self.indent(icount + 2) + "} while (0)"
    504528                return self.indent(icount) + "void " + pfx + "do_segment(" + parameters + ") {" \
    505                 + "\n" + self.indent(icount) + "  for (int i = 0; i < SEGMENT_BLOCKS; i++)" \
     529                + "\n" + self.indent(icount) + "  int i;" \
     530                + "\n" + self.indent(icount) + "  for (i = 0; i < SEGMENT_BLOCKS; i++)" \
    506531                + "\n" + self.indent(icount) + "    " + pfx + "do_block(" + do_block_call_args + ");" \
    507532                + "\n" + self.indent(icount + 2) + "}"
     
    526551        def do_block_parameters(self, parameters):
    527552                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])
     553                        #return ", ".join([self.type_name + " * " + self.instance_name] + [upper1(p) + " * " + lower1(p) for p in parameters])
     554                        return ", ".join([lower1(self.type_name)] + [lower1(p) for p in parameters])
    529555                else: return ", ".join([upper1(p) + " & " + lower1(p) for p in parameters])
    530556               
    531557        def do_final_block_parameters(self, parameters):
    532558                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"])
     559                        #return ", ".join([self.type_name + " * " + self.instance_name] + [upper1(p) + " * " + lower1(p) for p in parameters]+ ["BitBlock EOF_mask"])
     560                        return ", ".join([lower1(self.type_name)] + [lower1(p) for p in parameters]+ ["EOF_mask"])
    534561                else: return ", ".join([upper1(p) + " & " + lower1(p) for p in parameters]+ ["BitBlock EOF_mask"])
    535562               
    536563        def do_segment_parameters(self, parameters):
    537564                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])
     565                        #return ", ".join([self.type_name + " * " + + self.instance_name] + [upper1(p) + " " + lower1(p) + "[]" for p in parameters])
     566                        return ", ".join([lower1(self.type_name)] + [lower1(p) for p in parameters])
    539567                else: return ", ".join([upper1(p) + " " + lower1(p) + "[]" for p in parameters])
    540568
     
    583611                final_block_node = copy.deepcopy(node)
    584612                if self.use_C_syntax:
    585                         carryQname = stream_function.instance_name + "->carryQ"
     613                        carryQname = stream_function.instance_name + ".carryQ"
    586614                else: carryQname = "carryQ"
    587615                CarryIntro(carryQname).xfrm_fndef(node)
     
    634662        self.Cinits += self.emitter.carry_init(self.main_carry_count)
    635663    self.Cinits += StreamInitializations().xfrm(self.main_module)
    636    
    637     for key in self.stream_functions.keys():
    638                 self.Cinits += self.emitter.declaration(self.stream_functions[key].type_name, self.stream_functions[key].instance_name, 2)
    639664    if self.use_C_syntax:
    640665                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)
     666                        if self.stream_functions[key].carry_count == 0: continue
     667                        self.Cinits += self.emitter.declaration(self.stream_functions[key].type_name, self.stream_functions[key].instance_name, 2)
     668                        self.Cinits += "CarryInit(" + self.stream_functions[key].instance_name + ".carryQ, %i);\n" % (self.stream_functions[key].carry_count)
     669    else:
     670                for key in self.stream_functions.keys():
     671                        self.Cinits += self.emitter.declaration(self.stream_functions[key].type_name, self.stream_functions[key].instance_name, 2)
    642672
    643673                       
Note: See TracChangeset for help on using the changeset viewer.