def _make_accessors(self, info): """Build get() and set() function based on info. """ name = self._array_name(info) fname = self._make_getter_name(info) size = info.sizeof() params = [("int", f"x{i}") for i in range(len(info.size))] mults = [ BinaryOp("*", a, ID(b[1])) for a, b in zip(info.size[1:], params[:-1]) ] offset = ID(params[-1][1]) for m in mults: offset = BinaryOp("+", m, offset) cases = [ Case(Constant("int", str(i)), [Return(ID(f"{name}_{i}"))]) for i in range(size) ] body = Compound([Switch(offset, Compound(cases))]) self.accessors.add(make_function(info.type, fname, params, body)) cases = [ Case(Constant("int", str(i)), [Assignment("=", ID(f"{name}_{i}"), ID("value")), Break()]) for i in range(size) ] body = Compound([Switch(offset, Compound(cases))]) type_ = (info.type.name if type(info.type) == Struct else info.type.names[0]) setter = make_function(IdentifierType(["void"]), fname.replace("get", "set", 1), params + [(type_, "value")], body) self.accessors.add(setter)
def visit_Assignment(self, pyc_assign: Assignment): # need to do some trick of +=, -=, *=, /= if len(pyc_assign.op) > 1: op = pyc_assign.op[:-1] if op == '+' or op == '-' or op == '*' or op == '/': new_op = BinaryOp(op, pyc_assign.lvalue, pyc_assign.rvalue) else: raise Exception("does not support operator: ", pyc_assign.op) pyc_assign.op = '=' pyc_assign.rvalue = new_op # create Big-O AST assign node assign_node = AssignNode() self.set_coordinate(assign_node, pyc_assign.coord) assign_node.target = self.visit(pyc_assign.lvalue) assign_node.value = self.visit(pyc_assign.rvalue) return assign_node
def cached_provider_call(self): provider = self.provider entity_flag = self.memo_flag_name return If(cond=UnaryOp(op='!', expr=ID(name=entity_flag)), iftrue=Compound(block_items=[ FuncCall(name=ID(name=provider), args=None), Assignment(op='=', lvalue=ID(name=entity_flag), rvalue=Constant(type='bool', value='true'))]), iffalse=None)
def create_new_assign(old_cond, new_cond, coord): global coord_aux coord_aux -= 1 # coord = old_cond.coord new_coord = Coord(coord.file, coord.line, coord.column) new_coord.line = coord_aux new_coord.column = coord_aux # print type(new_cond), type(new_cond) assign = Assignment('=', ID(new_cond), ID(old_cond), new_coord) return assign
def visit_Opcode(self, node): pOp_assignment = Assignment('=', ID("pOp"), ArrayRef(ID("&aOp"), ID("pos"))) try: node.stmts[0].block_items = [pOp_assignment] + node.stmts[0].block_items except Exception as e: logger = logging.getLogger("OpGen") logger.error("Opcode implementation not enclosed in a block!") code = self.visit(node) if self.debug_mode: code = ["__NL__"+line+"\\" for line in code.split("\n") if line.strip() != ";"] else: code = [line+"\\" for line in code.split("\n") if line.strip() != ";"] code_str = "\n".join(code[1:])[:-1] return self.get_header() + code_str+'; \n'
def visit_UnaryOp(self, pyc_unary_op: UnaryOp): # convert pyc_unary operator to pyc_assignment op = pyc_unary_op.op if op == '++' or op == 'p++': op = '+' elif op == '--' or op == 'p--': op = '-' elif op == '+' or op == '-': bin_op = BinaryOp(op, Constant('int', '0', pyc_unary_op.coord), pyc_unary_op.expr, pyc_unary_op.coord) return self.visit(bin_op) # else: # raise NotImplementedError('op=', op) right = BinaryOp(op, pyc_unary_op.expr, Constant('int', '1', pyc_unary_op.coord), pyc_unary_op.coord) pyc_assign = Assignment('=', pyc_unary_op.expr, right, pyc_unary_op.coord) return self.visit(pyc_assign)