Ignore:
Timestamp:
Apr 13, 2013, 10:58:50 AM (6 years ago)
Author:
cameron
Message:

Factor out AST helpers to mkast.py

File:
1 edited

Legend:

Unmodified
Added
Removed
  • proto/Compiler/CCGO_HMCPS.py

    r2802 r3017  
    88# Licensed under Open Software License 3.0
    99#
    10 import ast
     10import ast, mkast
    1111import carryInfo, CCGO
    1212
     
    1818      assert isinstance(testExpr.func, ast.Name)
    1919      assert testExpr.func.id == 'bitblock::any'
    20       testExpr.args[0] = make_call('simd_or', [bitBlockExpr, testExpr.args[0]])
     20      testExpr.args[0] = mkast.call('simd_or', [bitBlockExpr, testExpr.args[0]])
    2121      return testExpr
    2222    else:
    23       return ast.BinOp(testExpr, ast.BitOr(), make_call('bitblock::any', [bitBlockExpr]))
     23      return ast.BinOp(testExpr, ast.BitOr(), mkast.call('bitblock::any', [bitBlockExpr]))
    2424
    2525def TestHelper_Integer_Or(testExpr, intExpr):
    2626    return ast.BinOp(testExpr, ast.BitOr(), intExpr)
    2727
    28 def mk_var(var, mode=ast.Load()):
    29   if isinstance(var, str):
    30         var = ast.Name(var, mode)
    31   return var
    32  
    33 def make_assign(var, expr):
    34    if isinstance(var, str):
    35         var = ast.Name(var, ast.Store())
    36    return ast.Assign([var], expr)
    37 
    38 def make_index(var, num, mode=ast.Load()):
    39   if isinstance(var, str):
    40         var = ast.Name(var, ast.Load())
    41   return ast.Subscript(var, ast.Index(ast.Num(num)), mode)
    42 
    43 def make_index_store(var, num):
    44   if isinstance(var, str):
    45         var = ast.Name(var, ast.Load())
    46   return ast.Subscript(var, ast.Index(ast.Num(num)), ast.Store())
    47 
    48 def make_att(var, att, mode=ast.Load()):
    49   if isinstance(var, str):
    50         var = ast.Name(var, ast.Load())
    51   return ast.Attribute(var, att, mode)
    52 
    53 def make_att_store(var, att):
    54   if isinstance(var, str):
    55         var = ast.Name(var, ast.Load())
    56   return ast.Attribute(var, att, ast.Store())
    57 
    58 def make_call(fn_name, args):
    59   if isinstance(fn_name, str):
    60         fn_name = ast.Name(fn_name, ast.Load())
    61   return ast.Call(fn_name, args, [], None, None)
    62 
    63 def make_callStmt(fn_name, args):
    64   if isinstance(fn_name, str): fn_name = ast.Name(fn_name, ast.Load())
    65   return ast.Expr(ast.Call(fn_name, args, [], None, None))
    66 
    67 def make_mergeh(fw, x, y):
    68   return make_call("esimd<%i>::mergeh" % fw, [mk_var(x), mk_var(y)])
    69 
    70 def make_zero(fw):
    71   return make_call("simd<%i>::constant<0>" % fw, [])
    72 
    73  
     28
    7429#
    7530#
     
    218173    def carry_pack_full(self, ub, v = None, mode = ast.Load()):
    219174       if v == None: v = self.carryPackVar
    220        return make_att(make_index(v, ub), '_128', mode)
     175       return mkast.att(mkast.index(v, ub), '_128', mode)
    221176
    222177    def carry_pack_index(self, fw, ub, rp, mode = ast.Load()):
    223        return make_index(make_att(make_index(self.carryPackVar, ub), '_%i' % fw), rp, mode)
     178       return mkast.index(mkast.att(mkast.index(self.carryPackVar, ub), '_%i' % fw), rp, mode)
    224179
    225180    def local_pack_full(self, ub, mode = ast.Load()):
     
    228183    def local_pack_index(self, fw, ub, rp, mode = ast.Load()):
    229184       v = "sub" + self.carryPackVar
    230        return make_index(make_att(make_index(v, ub), '_%i' % fw), rp, mode)
     185       return mkast.index(mkast.att(mkast.index(v, ub), '_%i' % fw), rp, mode)
    231186 
    232187
     
    242197      size = carry_last - carry_base + 1
    243198      if carry_last & size: 
    244         v1 = mk_var(self.cg_temp(carry_last, carry_base))
    245         v0 = mk_var(self.cg_temp(carry_last - size, carry_base - size))
    246         v2 = mk_var(self.cg_temp(carry_last, carry_base - size), ast.Store())
     199        v1 = mkast.var(self.cg_temp(carry_last, carry_base))
     200        v0 = mkast.var(self.cg_temp(carry_last - size, carry_base - size))
     201        v2 = mkast.var(self.cg_temp(carry_last, carry_base - size), ast.Store())
    247202        assigs = []
    248         if add_decl: assigs.append(make_callStmt('BitBlock_declare', [v2]))
    249         assigs.append(make_assign(v2, make_mergeh(self.fw * size, v1, v0)))
     203        if add_decl: assigs.append(mkast.callStmt('BitBlock_declare', [v2]))
     204        assigs.append(mkast.assign(v2, mkast.mergeh(self.fw * size, v1, v0)))
    250205        return assigs + self.gen_merges(carry_last, carry_base - size, add_decl)
    251206      else: return []
     
    278233        #
    279234        if zero_count == 1: 
    280           v = mk_var(self.cg_temp(carry_num))
    281           if add_decl: stmts.append(make_callStmt('BitBlock_declare', [v]))
    282           stmts.append(make_assign(v, make_zero(self.fw)))
     235          v = mkast.var(self.cg_temp(carry_num))
     236          if add_decl: stmts.append(mkast.callStmt('BitBlock_declare', [v]))
     237          stmts.append(mkast.assign(v, mkast.zero(self.fw)))
    283238          return stmts
    284239        else:
    285240          zero_count_floor = pow2floor(zero_count)
    286241          hi_num = carry_num + zero_count_floor
    287           v = mk_var(self.cg_temp(hi_num - 1, carry_num))
    288           if add_decl: stmts.append(make_callStmt('BitBlock_declare', [v]))
    289           stmts.append(make_assign(v, make_zero(self.fw)))
     242          v = mkast.var(self.cg_temp(hi_num - 1, carry_num))
     243          if add_decl: stmts.append(mkast.callStmt('BitBlock_declare', [v]))
     244          stmts.append(mkast.assign(v, mkast.zero(self.fw)))
    290245          remaining_zeroes = zero_count - zero_count_floor
    291246          return stmts + self.gen_multiple_carry_zero_then_pack(hi_num, remaining_zeroes, add_decl) 
    292247      #
    293       shift_result = mk_var(self.cg_temp(carry_num + shift - 1, pending_carry_base))
     248      shift_result = mkast.var(self.cg_temp(carry_num + shift - 1, pending_carry_base))
    294249      pending = self.cg_temp(carry_num - 1, pending_carry_base)
    295       #a1 = make_assign(shift_result, make_call('bitblock::srli<%i>' % (self.fw * shift), [mk_var(pending)]))
    296       if add_decl: stmts.append(make_callStmt('BitBlock_declare', [shift_result]))
    297       stmts.append(make_assign(shift_result, make_call('mvmd<%i>::srli<%i>' % (self.fw, shift), [mk_var(pending)])))
     250      #a1 = mkast.assign(shift_result, mkast.call('bitblock::srli<%i>' % (self.fw * shift), [mkast.var(pending)]))
     251      if add_decl: stmts.append(mkast.callStmt('BitBlock_declare', [shift_result]))
     252      stmts.append(mkast.assign(shift_result, mkast.call('mvmd<%i>::srli<%i>' % (self.fw, shift), [mkast.var(pending)])))
    298253      # Do any necessary merges
    299254      m = self.gen_merges(carry_num + shift - 1,  pending_carry_base, add_decl)
     
    367322        ub = posn/self.field_count
    368323        rp = posn%self.field_count
    369         return make_call("convert", [self.carry_pack_index(self.fw, ub, rp)])
     324        return mkast.call("convert", [self.carry_pack_index(self.fw, ub, rp)])
    370325    def GenerateCarryOutStore(self, operation_no, carry_out_expr):
    371326        block_no = self.carryInfoSet.containing_block[operation_no]
     
    375330        # Save the carry in the carry temp variable and then merge
    376331        # pending carry temps as far as possible.
    377         assigs = [make_assign(self.temp_prefix + repr(rp), carry_out_expr)]
     332        assigs = [mkast.assign(self.temp_prefix + repr(rp), carry_out_expr)]
    378333        assigs += self.gen_merges(rp, rp)
    379334        # Only generate an actual store for the last carryout in a pack.
     
    386341        if next_posn % self.field_count == 0:
    387342          shift_op = "simd<%i>::srli<%i>" % (self.fw, self.fw-1)
    388           storable_carry_in_form = make_call(shift_op, [mk_var(self.cg_temp(self.field_count - 1, 0))])
    389           assigs.append(make_assign(self.carry_pack_full(ub, mode = ast.Store()), storable_carry_in_form))
     343          storable_carry_in_form = mkast.call(shift_op, [mkast.var(self.cg_temp(self.field_count - 1, 0))])
     344          assigs.append(mkast.assign(self.carry_pack_full(ub, mode = ast.Store()), storable_carry_in_form))
    390345        return assigs
    391346    def GenerateAdvanceInAccess(self, operation_no):
     
    393348    def GenerateAdvanceOutStore(self, operation_no, adv_out_expr):
    394349        return [ast.Assign([self.carry_pack_full(self.carryPack_count + self.adv_n_map[operation_no], mode=ast.Store())],
    395                            make_call("bitblock::srli<64>", [adv_out_expr]))]
     350                           mkast.call("bitblock::srli<64>", [adv_out_expr]))]
    396351    def GenerateTestAll(self, instance_name):
    397352        if self.totalPack_count == 0: return ast.Num(0)
    398353        else:
    399             v = make_att(instance_name, self.carryPackVar)
     354            v = mkast.att(instance_name, self.carryPackVar)
    400355            t = self.carry_pack_full(0, v)
    401356            for i in range(1, self.totalPack_count):
    402357              t2 = self.carry_pack_full(i, v)
    403               t = make_call('simd_or', [t, t2])
    404             return make_call('bitblock::any', [t])
     358              t = mkast.call('simd_or', [t, t2])
     359            return mkast.call('bitblock::any', [t])
    405360    def GenerateTest(self, block_no, testExpr):
    406361        posn = self.block_base[block_no] - self.carry_offset
     
    416371            for i in range(1, count/self.field_count):
    417372              v2 = self.carry_pack_full(ub + i)
    418               t = make_call('simd_or', [t, v2])
     373              t = mkast.call('simd_or', [t, v2])
    419374            return TestHelper_Bitblock_Or(testExpr, t)
    420375    def GenerateCarryIfTest(self, block_no, ifTest):
     
    437392        if (end_pos + 1) % self.field_count == 0:
    438393          shift_op = "simd<%i>::srli<%i>" % (self.fw, self.fw-1)
    439           storable_carry_in_form = make_call(shift_op, [mk_var(self.cg_temp(self.field_count - 1, 0))])
    440           assigs.append(make_assign(self.carry_pack_full(ub, mode = ast.Store()), storable_carry_in_form))
     394          storable_carry_in_form = mkast.call(shift_op, [mkast.var(self.cg_temp(self.field_count - 1, 0))])
     395          assigs.append(mkast.assign(self.carry_pack_full(ub, mode = ast.Store()), storable_carry_in_form))
    441396        return assigs
    442397
     
    446401        if count >= self.field_count:
    447402          ub_count = count / self.field_count
    448           decls = [make_callStmt('ubitblock_declare', [mk_var('sub' + self.carryPackVar), ast.Num(ub_count)])]
     403          decls = [mkast.callStmt('ubitblock_declare', [mkast.var('sub' + self.carryPackVar), ast.Num(ub_count)])]
    449404          count = self.field_count
    450405        else: decls = []
     
    458413          s = s * 2
    459414        #return BitBlock_decls_from_vars(decls)
    460         return decls + [make_callStmt('BitBlock_declare', [mk_var(t)]) for t in temps]
     415        return decls + [mkast.callStmt('BitBlock_declare', [mkast.var(t)]) for t in temps]
    461416   
    462417    def GenerateCarryWhileTest(self, block_no, testExpr):
     
    481436          v0 = self.cg_temp(rp + count - 1, rp)
    482437          lv0 = self.local_temp(count - 1, 0)
    483           return [make_assign(v0, make_call('simd_or', [mk_var(v0), mk_var(lv0)]))]
     438          return [mkast.assign(v0, mkast.call('simd_or', [mkast.var(v0), mkast.var(lv0)]))]
    484439        n = (count+self.field_count-1)/self.field_count
    485440        assigs = []
    486441        for i in range(n):
    487           assigs.append(make_assign(self.carry_pack_full(ub + i, mode = ast.Store()), make_call('simd_or', [self.carry_pack_full(ub + i), self.local_pack_full(i)])))
     442          assigs.append(mkast.assign(self.carry_pack_full(ub + i, mode = ast.Store()), mkast.call('simd_or', [self.carry_pack_full(ub + i), self.local_pack_full(i)])))
    488443        return assigs
    489444    def GenerateStreamFunctionFinalization(self):
     
    501456        ub = posn/self.field_count
    502457        rp = posn%self.field_count
    503         #return make_call("convert", [self.carry_pack_index(self.fw, ub, rp)])
     458        #return mkast.call("convert", [self.carry_pack_index(self.fw, ub, rp)])
    504459        if rp == 0: e = self.carry_pack_full(ub)
    505         else: e = make_call("mvmd<%i>::srli<%i>" %(self.fw, rp), [self.carry_pack_full(ub)])
     460        else: e = mkast.call("mvmd<%i>::srli<%i>" %(self.fw, rp), [self.carry_pack_full(ub)])
    506461        if rp == self.field_count - 1:
    507462          return e
    508         else: return make_call('simd_and', [e, mk_var("simd_const_1")])
     463        else: return mkast.call('simd_and', [e, mkast.var("simd_const_1")])
    509464
    510465#
     
    515470    def carry_pack_full(self, ub, v = None, mode = ast.Load()):
    516471       if v == None: v = self.carryPackVar
    517        return make_index(v, ub, mode)
     472       return mkast.index(v, ub, mode)
    518473
    519474    def carry_pack_index(self, fw, ub, rp, mode = ast.Load()):
    520        return make_call("mvmd<%i>::extract<%i>" % (fw, rp), [self.carry_pack_full(ub)])
     475       return mkast.call("mvmd<%i>::extract<%i>" % (fw, rp), [self.carry_pack_full(ub)])
    521476
    522477    def GenerateCarryDecls(self):
     
    542497        if count >= self.field_count:
    543498          ub_count = count / self.field_count
    544           decls = [make_callStmt('BitBlock_declare', [self.local_pack_full(ub_count)])]
    545           decls += [make_assign(self.local_pack_full(i, ast.Store()), make_zero(self.fw)) for i in range(ub_count)]
     499          decls = [mkast.callStmt('BitBlock_declare', [self.local_pack_full(ub_count)])]
     500          decls += [mkast.assign(self.local_pack_full(i, ast.Store()), mkast.zero(self.fw)) for i in range(ub_count)]
    546501          count = self.field_count
    547502        else: decls = []
     
    555510          s = s * 2
    556511        #return BitBlock_decls_from_vars(decls)
    557         return decls + [make_callStmt('BitBlock_declare', [mk_var(t)]) for t in temps]
     512        return decls + [mkast.callStmt('BitBlock_declare', [mkast.var(t)]) for t in temps]
    558513
    559514#
     
    581536        rp = posn%self.BLOCK_SIZE
    582537        if rp == 0: e = self.carry_pack_full(pk)
    583         elif rp < self.BLOCK_SIZE/2: e = make_call("simd<%i>::srli<%i>" %(self.BLOCK_SIZE/2, rp), [self.carry_pack_full(pk)])
    584         else: e = make_call("bitblock::srli<%i>" %(rp), [self.carry_pack_full(pk)])
     538        elif rp < self.BLOCK_SIZE/2: e = mkast.call("simd<%i>::srli<%i>" %(self.BLOCK_SIZE/2, rp), [self.carry_pack_full(pk)])
     539        else: e = mkast.call("bitblock::srli<%i>" %(rp), [self.carry_pack_full(pk)])
    585540        if rp == self.BLOCK_SIZE - 1:
    586541          return e
    587         else: return make_call('simd_and', [e, mk_var("simd_const_1")])
     542        else: return mkast.call('simd_and', [e, mkast.var("simd_const_1")])
    588543
    589544
     
    594549        # Save the carry in the carry temp variable and then merge
    595550        # pending carry temps as far as possible.
    596         assigs = [make_assign(self.temp_prefix + repr(rp), carry_out_expr)]
     551        assigs = [mkast.assign(self.temp_prefix + repr(rp), carry_out_expr)]
    597552        assigs += self.gen_merges(rp, rp)
    598553        # Only generate an actual store for the last carryout in a pack.
     
    608563          fd = (posn%self.BLOCK_SIZE)/self.field_count
    609564          mask_op = "hsimd<%i>::signmask" % (self.fw)
    610           storable_carry_in_form = make_call(mask_op, [mk_var(self.cg_temp(self.field_count - 1, 0))])
    611           assigs.append(make_assign(self.carry_pack_index(self.field_count, pk, fd, mode = ast.Store()), storable_carry_in_form))
     565          storable_carry_in_form = mkast.call(mask_op, [mkast.var(self.cg_temp(self.field_count - 1, 0))])
     566          assigs.append(mkast.assign(self.carry_pack_index(self.field_count, pk, fd, mode = ast.Store()), storable_carry_in_form))
    612567        return assigs
    613568
     
    679634        end_pos = (self.alloc_map[next_op]  - self.carry_offset - 1) % self.field_count
    680635        #print rp, next_op,self.alloc_map[next_op]
    681         #assigs = [make_assign(self.cg_temp(end_pos, rp), make_zero(self.fw))]
     636        #assigs = [mkast.assign(self.cg_temp(end_pos, rp), mkast.zero(self.fw))]
    682637        assigs = self.gen_multiple_carry_zero_then_pack(rp, end_pos - rp + 1)
    683638        if (end_pos + 1) % self.field_count == 0:
     
    685640          fd = (posn%self.BLOCK_SIZE)/self.field_count
    686641          mask_op = "hsimd<%i>::signmask" % (self.fw)
    687           storable_carry_in_form = make_call(mask_op, [mk_var(self.cg_temp(self.field_count - 1, 0))])
    688           assigs.append(make_assign(self.carry_pack_index(self.field_count, pk, fd, mode = ast.Store()), storable_carry_in_form))
     642          storable_carry_in_form = mkast.call(mask_op, [mkast.var(self.cg_temp(self.field_count - 1, 0))])
     643          assigs.append(mkast.assign(self.carry_pack_index(self.field_count, pk, fd, mode = ast.Store()), storable_carry_in_form))
    689644        return assigs
    690645
     
    697652          v0 = self.cg_temp(rp + sz - 1, rp)
    698653          lv0 = self.local_temp(sz - 1, 0)
    699           return [make_assign(v0, make_call('simd_or', [mk_var(v0), mk_var(lv0)]))]
     654          return [mkast.assign(v0, mkast.call('simd_or', [mkast.var(v0), mkast.var(lv0)]))]
    700655        local_posn = 0
    701656        pk = posn/self.BLOCK_SIZE
     
    708663          v0 = self.carry_pack_index(self.field_count, pk, fd)
    709664          lv0 = self.local_pack_index(self.field_count, local_pk, local_fd)
    710           assigs.append(make_assign([self.carry_pack_index(self.field_count, pk, fd, ast.Store())], ast.BinOp(v0, ast.BitOr(), lv0)))
     665          assigs.append(mkast.assign([self.carry_pack_index(self.field_count, pk, fd, ast.Store())], ast.BinOp(v0, ast.BitOr(), lv0)))
    711666          posn += self.field_count
    712667          local_posn += self.field_count
     
    786741        # Save the carry in the carry temp variable and then merge
    787742        # pending carry temps as far as possible.
    788         v = mk_var(self.temp_prefix + repr(rp))
     743        v = mkast.var(self.temp_prefix + repr(rp))
    789744        assigs = []
    790         if add_decl: assigs.append(make_callStmt('BitBlock_declare', [v]))
    791         assigs.append(make_assign(v, carry_out_expr))
     745        if add_decl: assigs.append(mkast.callStmt('BitBlock_declare', [v]))
     746        assigs.append(mkast.assign(v, carry_out_expr))
    792747        assigs += self.gen_merges(rp, rp, add_decl)
    793748        # Only generate an actual store for the last carryout in a pack.
     
    800755          mask_blk = (posn%self.BLOCK_SIZE)/self.field_count
    801756          mask_op = "hsimd<%i>::signmask" % (self.fw)
    802           storable_carry_in_form = make_call(mask_op, [mk_var(self.cg_temp(self.field_count - 1, 0))])
    803           assigs.append(make_assign(self.carry_pack_index(self.field_count, pk, mask_blk, mode = ast.Store()), storable_carry_in_form))
     757          storable_carry_in_form = mkast.call(mask_op, [mkast.var(self.cg_temp(self.field_count - 1, 0))])
     758          assigs.append(mkast.assign(self.carry_pack_index(self.field_count, pk, mask_blk, mode = ast.Store()), storable_carry_in_form))
    804759        return assigs
    805760
     
    808763        count = self.aligned_size[block_no]
    809764        ub_count = (count + self.BLOCK_SIZE - 1)/ self.BLOCK_SIZE
    810         decls = [make_callStmt('ubitblock_declare', [mk_var("sub" + self.carryPackVar), ast.Num(ub_count)])]
     765        decls = [mkast.callStmt('ubitblock_declare', [mkast.var("sub" + self.carryPackVar), ast.Num(ub_count)])]
    811766        return decls
    812767
     
    818773        if count >= self.BLOCK_SIZE:
    819774          for i in range(count/self.BLOCK_SIZE):
    820             assigs.append(make_assign(self.carry_pack_full(pk + i, mode = ast.Store()), make_call('simd_or', [self.carry_pack_full(pk + i), self.local_pack_full(i)])))
     775            assigs.append(mkast.assign(self.carry_pack_full(pk + i, mode = ast.Store()), mkast.call('simd_or', [self.carry_pack_full(pk + i), self.local_pack_full(i)])))
    821776
    822777        else:
    823778          rp = (posn%self.BLOCK_SIZE)/count
    824779          expr = ast.BinOp(self.carry_pack_index(count, pk, rp), ast.BitOr(), self.local_pack_index(count, 0, 0))
    825           assigs.append(make_assign(self.carry_pack_index(count, pk, rp, mode = ast.Store()), expr))
     780          assigs.append(mkast.assign(self.carry_pack_index(count, pk, rp, mode = ast.Store()), expr))
    826781        return assigs
    827782
Note: See TracChangeset for help on using the changeset viewer.