<jupyter_start><jupyter_text>An instructor is 1 op-code byte, followed by 0 or more operands.  The instruction encode type information about it's operands (eg: iadd to add int, ifloat to add floats, they have 2 different opcodes).  Several instructions has a prefix depending on the type of the operands- b: byte- s: short- i: int- l: long- f: float- d: double- c: char- a: referenceOther where type are obvious doesn't have one.  There isn't an instruction for every type. Especially for byte, short, it's usually needed to use some extra conversion instructions.  Load / Store- Tload: load local variable into operand stack- Tstore: store value into local variable from operand stack- Taload: load array value at index i into operand stack- Tastore: store value into array at index i from operand stack  Arithmetic- Tadd- Tsub- Tmul- Tdiv- Trem (modulo)- Tneg- Tshl (logical shift left)- Tshr (logical shift right)- Turshr (unsigned shift right)- Tor (bitwise or)- Tand (bitwise and)- Txor (bitwise xor)  Type conversionUsed for explicit conversion in user code, or because of the lack of instructions for some types.  Widening:- i2T: int to long, float or double- l2T: long to float, double- f2d: float to doubleNarrowing:- i2T: int to byte, short, char- l2i: long to int- f2T: float to int, long- d2T: double to float, int, long  Object creation and manipulation- new: create new instance of class- newarray: create new array- getfield: get instance field value- putfield: set instance field value- getstatic: get static field value- putstatic: put static field value- arraylength: get size of array<jupyter_code># %load ./examples/Vec2fNew.java
public class Vec2fNew
{
    private float _x;
    private float _y;

    public Vec2fNew(float x, float y)
    {
	_x = x;
	_y = y;
    }

    public static Vec2fNew new_vec(float x, float y)
    {
	return new Vec2fNew(x, y);
    }

}

# %load ./examples/Vec2fNew.code.txt
Compiled from "Vec2fNew.java"
public class Vec2fNew {
  public Vec2fNew(float, float);
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: fload_1
       6: putfield      #7                  // Field _x:F
       9: aload_0
      10: fload_2
      11: putfield      #13                 // Field _y:F
      14: return

  public static Vec2fNew new_vec(float, float);
    Code:
       0: new           #8                  // class Vec2fNew
       3: dup
       4: fload_0
       5: fload_1
       6: invokespecial #16                 // Method "<init>":(FF)V
       9: areturn
}

# %load ./examples/Vec2fGetSet.java
public class Vec2fGetSet
{
    private float _x;
    private float _y;

    public float getX() { return _x; }
    public float getY() { return _y; }
    public void setX(float x) { _x = x; }
    public void setY(float y) { _y = y; }
}

# %load ./examples/Vec2fGetSet.code.txt
Compiled from "Vec2fGetSet.java"
public class Vec2fGetSet {
  public Vec2fGetSet();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public float getX();
    Code:
       0: aload_0
       1: getfield      #7                  // Field _x:F
       4: freturn

  public float getY();
    Code:
       0: aload_0
       1: getfield      #13                 // Field _y:F
       4: freturn

  public void setX(float);
    Code:
       0: aload_0
       1: fload_1
       2: putfield      #7                  // Field _x:F
       5: return

  public void setY(float);
    Code:
       0: aload_0
       1: fload_1
       2: putfield      #13                 // Field _y:F
       5: return
}

# %load ./examples/Counter.java
public class Counter
{

    private static int _instances;

    Counter()
    {
	++_instances;
    }

    public static int get_instances() { return _instances; }

}

# %load ./examples/Counter.code.txt
Compiled from "Counter.java"
public class Counter {
  Counter();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: getstatic     #7                  // Field _instances:I
       7: iconst_1
       8: iadd
       9: putstatic     #7                  // Field _instances:I
      12: return

  public static int get_instances();
    Code:
       0: getstatic     #7                  // Field _instances:I
       3: ireturn
}

# %load ./examples/MyVec.java
public final class MyVec
{

    private int _data[];
    private int _size;

    MyVec()
    {
	_data = new int[8];
	_size = 0;
    }

    public int getSize()
    {
	return _size;
    }

    public int getCapacity()
    {
	return _data.length;
    }

    public int getVal(int id)
    {
	return _data[id];
    }

    public void setVal(int id, int val)
    {
	_data[id] = val;
    }

    public void push(int val)
    {
	reserve(_size + 1);
	_data[_size] = val;
	++_size;
    }

    public void reserve(int new_cap)
    {
	int next_cap = getCapacity();
	while (new_cap > next_cap)
	    next_cap *= 2;
	if (next_cap == getCapacity())
	    return;
	
	int[] new_data = new int[next_cap];
	for (int i = 0; i < getSize(); ++i)
	    new_data[i] = _data[i];
	_data = new_data;
    }

}

# %load ./examples/MyVec.code.txt
Compiled from "MyVec.java"
public final class MyVec {
  MyVec();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: bipush        8
       7: newarray       int
       9: putfield      #7                  // Field _data:[I
      12: aload_0
      13: iconst_0
      14: putfield      #13                 // Field _size:I
      17: return

  public int getSize();
    Code:
       0: aload_0
       1: getfield      #13                 // Field _size:I
       4: ireturn

  public int getCapacity();
    Code:
       0: aload_0
       1: getfield      #7                  // Field _data:[I
       4: arraylength
       5: ireturn

  public int getVal(int);
    Code:
       0: aload_0
       1: getfield      #7                  // Field _data:[I
       4: iload_1
       5: iaload
       6: ireturn

  public void setVal(int, int);
    Code:
       0: aload_0
       1: getfield      #7                  // Field _data:[I
       4: iload_1
       5: iload_2
       6: iastore
       7: return

  public void push(int);
    Code:
       0: aload_0
       1: aload_0
       2: getfield      #13                 // Field _size:I
       5: iconst_1
       6: iadd
       7: invokevirtual #17                 // Method reserve:(I)V
      10: aload_0
      11: getfield      #7                  // Field _data:[I
      14: aload_0
      15: getfield      #13                 // Field _size:I
      18: iload_1
      19: iastore
      20: aload_0
      21: dup
      22: getfield      #13                 // Field _size:I
      25: iconst_1
      26: iadd
      27: putfield      #13                 // Field _size:I
      30: return

  public void reserve(int);
    Code:
       0: aload_0
       1: invokevirtual #21                 // Method getCapacity:()I
       4: istore_2
       5: iload_1
       6: iload_2
       7: if_icmple     17
      10: iload_2
      11: iconst_2
      12: imul
      13: istore_2
      14: goto          5
      17: iload_2
      18: aload_0
      19: invokevirtual #21                 // Method getCapacity:()I
      22: if_icmpne     26
      25: return
      26: iload_2
      27: newarray       int
      29: astore_3
      30: iconst_0
      31: istore        4
      33: iload         4
      35: aload_0
      36: invokevirtual #25                 // Method getSize:()I
      39: if_icmpge     59
      42: aload_3
      43: iload         4
      45: aload_0
      46: getfield      #7                  // Field _data:[I
      49: iload         4
      51: iaload
      52: iastore
      53: iinc          4, 1
      56: goto          33
      59: aload_0
      60: aload_3
      61: putfield      #7                  // Field _data:[I
      64: return
}<jupyter_output><empty_output><jupyter_text>Operand stack management- pop: pop and ignore top value- pop2: pop and ignore top 2 values- dup: duplicate top value- dup2: duplicate top 2 values- swap: stap top 2 stack values  Conditional branchingifcond or if_cond (eg ifeq, ifne, if_icmple)Used for if/else and while code  Unconditional branching- goto: jump to some code in the function using a branch offset- goto_w: same as offset, but allowing bigger offset.- jsr: jump subroutine: jump to some code using a branch offset, and push return adress in stack- ret: return from a subroutine<jupyter_code># %load ./examples/Branching.java
public class Branching
{

    public static void test_if(int p)
    {
	int x;
	if (p > 3)
	    x = 5;
    }

    public static void test_if_else(int p)
    {
	int x;
	if (p == 3)
	    x = 5;
	else
	    x = 7;
    }

    public static void test_while(int[] data)
    {
	if (data == null)
	    return;
	int i = 0;
	while (i < 4)
	{
	    i += 5;
	    data[0] = 6;
	}
    }

}

# %load ./examples/Branching.code.txt
Compiled from "Branching.java"
public class Branching {
  public Branching();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void test_if(int);
    Code:
       0: iload_0
       1: iconst_3
       2: if_icmple     7
       5: iconst_5
       6: istore_1
       7: return

  public static void test_if_else(int);
    Code:
       0: iload_0
       1: iconst_3
       2: if_icmpne     10
       5: iconst_5
       6: istore_1
       7: goto          13
      10: bipush        7
      12: istore_1
      13: return

  public static void test_while(int[]);
    Code:
       0: aload_0
       1: ifnonnull     5
       4: return
       5: iconst_0
       6: istore_1
       7: iload_1
       8: iconst_4
       9: if_icmpge     23
      12: iinc          1, 5
      15: aload_0
      16: iconst_0
      17: bipush        6
      19: iastore
      20: goto          7
      23: return
}<jupyter_output><empty_output><jupyter_text>Method invocation- invokevirtual: call an instance method, performing dynamic dispatch- invokeinterface: call an interface method- invokespecial: special method, such as initialization method.- invokestatic: call a static methodWhen calling a method, we pass arguments on the stack.  We return from a method using instruction Treturn, or just return for void functions.  The value we want to return is stored on top of the stack.  When returning from a method, the return value is on top of the stack.<jupyter_code># %load ./examples/Fact.java
public class Fact
{

    public static int get(int x)
    {
	if (x <= 1)
	    return 1;
	else
	    return x * Fact.get(x - 1);
    }

}

# %load ./examples/Fact.code.txt
Compiled from "Fact.java"
public class Fact {
  public Fact();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static int get(int);
    Code:
       0: iload_0
       1: iconst_1
       2: if_icmpgt     7
       5: iconst_1
       6: ireturn
       7: iload_0
       8: iload_0
       9: iconst_1
      10: isub
      11: invokestatic  #7                  // Method get:(I)I
      14: imul
      15: ireturn
}<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08863997177632739, 'fraction_numerical': 0.03933674369377315, 'mean_word_length': 1.7528526341344988, 'pattern_counts': {'":': 7, '<': 23, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '470516', 'license': 'MIT', 'n_tokens_mistral': 3960, 'n_tokens_neox': 3739, 'n_words': 1268, 'path': 'refs/jvm/instructions.ipynb', 'repo_name': 'obs145628/ml-notebooks', 'size': '11338'}