1 /*
   2  * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package vm.compiler.coverage.parentheses.share;
  24 
  25 import java.util.List;
  26 
  27 /**
  28  * Tiny stack InstructionsExecutor. This executor verifies HotspotInstructionsExecutor
  29  */
  30 public class TinyInstructionsExecutor implements InstructionsExecutor {
  31 
  32     private int[] stack;
  33     private int stackHead = -1;
  34 
  35     public TinyInstructionsExecutor(int stackSize) {
  36         stack = new int[stackSize];
  37     }
  38 
  39     private void putOnStack(int v) {
  40         stack[++stackHead] = v;
  41     }
  42 
  43     private void exec(Instruction instruction) {
  44         switch (instruction) {
  45             case ICONST_M1:
  46                 putOnStack(-1);
  47                 break;
  48             case ICONST_0:
  49                 putOnStack(0);
  50                 break;
  51             case ICONST_1:
  52                 putOnStack(1);
  53                 break;
  54             case ICONST_2:
  55                 putOnStack(2);
  56                 break;
  57             case ICONST_3:
  58                 putOnStack(3);
  59                 break;
  60             case ICONST_4:
  61                 putOnStack(4);
  62                 break;
  63             case ICONST_5:
  64                 putOnStack(5);
  65                 break;
  66             case DUP:
  67                 stack[stackHead + 1] = stack[stackHead];
  68                 stackHead++;
  69                 break;
  70 
  71             case IADD:
  72                 stack[stackHead - 1] += stack[stackHead];
  73                 stackHead--;
  74                 break;
  75             case ISUB:
  76                 stack[stackHead - 1] -= stack[stackHead];
  77                 stackHead--;
  78                 break;
  79             case IMUL:
  80                 stack[stackHead - 1] *= stack[stackHead];
  81                 stackHead--;
  82                 break;
  83             case IOR:
  84                 stack[stackHead - 1] |= stack[stackHead];
  85                 stackHead--;
  86                 break;
  87             case IAND:
  88                 stack[stackHead - 1] &= stack[stackHead];
  89                 stackHead--;
  90                 break;
  91             case IXOR:
  92                 stack[stackHead - 1] ^= stack[stackHead];
  93                 stackHead--;
  94                 break;
  95             case ISHL:
  96                 stack[stackHead - 1] <<= stack[stackHead];
  97                 stackHead--;
  98                 break;
  99             case ISHR:
 100                 stack[stackHead - 1] >>= stack[stackHead];
 101                 stackHead--;
 102                 break;
 103 
 104             case SWAP: {
 105                 int t = stack[stackHead];
 106                 stack[stackHead] = stack[stackHead - 1];
 107                 stack[stackHead - 1] = t;
 108                 break;
 109             }
 110             case NOP:
 111                 break;
 112             case INEG:
 113                 stack[stackHead] = -stack[stackHead];
 114                 break;
 115         }
 116     }
 117 
 118     private int top() {
 119         return stack[stackHead];
 120     }
 121 
 122     @Override
 123     public int execute(List<Instruction> instructions) {
 124         for (Instruction instruction : instructions) {
 125             exec(instruction);
 126         }
 127         return top();
 128     }
 129 }