def __ins_node__()

in preprocess/asm_mips.py [0:0]


  def __ins_node__(self):
    def prep(lines):
      pc = -4
      self.pcmap = {}
      self.pcmap_Name={}
      self.pcmap_Rev={}
      flag = 0
      filter_lines = []
      b = -1
      for ind, line in enumerate(self.lines):
        if "main:" in line:
            flag = 1
        if ("leave" in line or "ret" in line) and flag == 1:
            break
        if flag == 1:
          lsplit = line.split()
          if '$BB' in lsplit[0]:
              pc+=4
              b +=1
              self.pcmap[lsplit[0][:-1]] = pc
              self.pcmap_Name[lsplit[0][:-1]] = str(b)
              filter_lines.append(line)
          for ii, op in enumerate(lsplit):
            if (ii == 0) and (op not in self.op2id) and ('$BB' not in op)  \
                 and ('#' not in op) and ('.' not in op) and ('main' not in op):
              print(op + ' not supported!')
            if op in self.op2id:
              filter_lines.append(line)
              pc+=4
      return  filter_lines

    pc = -4
    i = -1
    b = -1
    self.lines = prep(self.lines)
    for ind, line in enumerate(self.lines):
      flag = 0
      op_id = -1
      lsplit = line.split()

      for op in lsplit:
        if op in self.op2id:
          flag = 1
          op_id = self.op2id[op]
          pc+=4
          i +=1
          break

      if '$BB' in lsplit[0]:
          flag = 1
          pc+=4
          i +=1
          b +=1

      if not flag:
        # if not flagged, this is a operation we are not supporting, add that
        print(line)
        continue
        if not isInt(line[-3]):
          raise Exception('Instruction need to be added')
        else:
          self.lines[i] += 'nop'
          op_id = self.op2id['nop']

      # add instruction node
      if '$BB' in lsplit[0]:
        node = Node(name=lsplit[0][:-1],
                      node_id=self.num_nodes,
                      node_type='ins',
                      ins_id=i,
                      pc=str(pc))

      else:
        node = Node(name='ins_'+self.id2op[op_id],
                    node_id=self.num_nodes,
                    node_type='ins',
                    ins_id=i,
                    pc=str(pc))

      # add subarray for this pc and add node
      self.nodes.append([])
      self.nodes[i].append(node)

      # build pc to node mapping for building jump edges
      self.pc2node[str(pc)] = node

      # for selecting subgraphs
      self.id2node[self.num_nodes] = node
      self.num_nodes += 1

      # add control-flow edges for next instructions
      if i > 0:
        self.__add_edge__(Edge(name='',
                               edge_type=self.edge_type_2_id['next-ins'],
                               src=self.nodes[i-1][0].id,
                               tgt=self.nodes[i][0].id))

    # add control-flow edges for jump instructions in self.op_types[0]
    for i, line in enumerate(self.lines):
      op  = line.split()[0]
      if  op in self.br_types and len(line.split()) > 1 and '$BB' in line:
        trg_br = line.split()[-1].strip()

        if '$BB' in trg_br:
          pc = self.pcmap[trg_br]
          assert (self.pc2node[str(pc)].name == trg_br), 'pc indexing incorrect!'
          if str(pc) in self.pc2node.keys():
            self.__add_edge__(Edge(name='',
                                    edge_type=self.edge_type_2_id['next-ins'],
                                    src=self.nodes[i][0].id,
                                    tgt=self.pc2node[str(pc)].id))