I will explain the changes made to Jikes code in this file

-----------------------------------------------------------------------------
Algorithm: 

Our purpose is to add instrumentation to machine codes of a sample Java program, so that
it generates trace as the machine codes are executed. After each memory access or instruction executed, 
we will insert a sequence of machine instructions which will print that memory address, or instruction.

machineCodes[] is an array having machine codes of the java program bein compiled, and mi
is the index of current position in that array. We will br using Jikes function "setMachineCodes()" to
add machine codes to this array.

-------------------------------------------------------------------------------------------

Important Files(Files in which major changes were made)

Assembler.java:   This is the file with code for baseline compiler. Not that this file is generated using 2 
files, Assembler.in and genAssembler.sh, thus to change Assembler.java, either of the 2 files needs to be changed.

VM.java
RuntimeEntrypoints.java
MainThread.java
TemplateCompilerFramework.java
BaselineCompiler.java
BaselineCompilerImpl.java

---------------------------------------------------------------------------------------------

Global Variables added

We have added some global variables in class VM(VM.java)

public static boolean start_instrument = A variable to regulate when to add instrumentation code
public static boolean vishal_flag = A variable to regulate when to add instrumentation code
public static int block_index = Id associated with the local code block bein compiled
public static int ins_index = offset of an instruction within the local code block
public static Address address_var = Address of the Jikes function we added for instruction trace ( instrument_fun() )
public static Address address_var_2 = Address of the Jikes function we added for memory trace ( instrument_add() )
public static Address address_var_3 = Address of the Jikes function we added for memory trace ( address_ins() )
public static int [] trace_array ;
public static int trace_index = 0;

----------------------------------------------------------------------------------------------

Description of functions added

--------------------------------------------------

1. Instrument_fun() added in file Assembler.java(changes made in file Assembler.in): This function inserts the sequence of machine
codes after every instruction executed. This machine code sequence then prints the instruction about to be executed. Currently, it
is printing 8 bytes of the instruction, can be easily extended to print 12 or 16 bytes

public final void instrument_fun () {
   
    if(VM.start_instrument && VM.vishal_flag){		\\ If the instrumentation is on
	
      GPR reg1 = ESP;					\\Declaring registers
      GPR reg2 = EAX;
      GPR reg3 = EIP;
      GPR reg4 = EDX;
      GPR reg5 = ECX;
      GPR reg6 = EBX;
      GPR reg7 = EDI;
      GPR reg8 = ESI;
        
	instrumented_code = false;			\\Turning the instrumentation off to avoid recursive instrumentation
	emitPUSH_Reg(reg2);				\\Saving registers on stack
	emitPUSH_Reg(reg4);
	emitPUSH_Reg(reg5);
	setMachineCodes(mi++, (byte) 0x9C);		\\Saving Flags on stack
	emitCALL_Imm(mi+5);				\\Call to next instruction, saves Program Counter of next instruction on stack
	emitPOP_Reg(reg2);				\\ Save PC in EAX reg
	emitADD_Reg_Imm(reg2, 37);			\\ Add 37 to EAX, to skip instructions added by our instrumentation code, go to 							\\ instruction which was initially next
	emitPUSH_RegInd (reg2);				\\ Push-indirect, i.e. the value at the address in EAX(first 4 bytes of instruction)
	emitADD_Reg_Imm(reg2, 4);			\\ Add 4 to EAX, to get next 4 bytes
	emitPUSH_RegInd (reg2);				\\ Push-indirect to get next 4 bytes
	emitMOV_Reg_RegDisp(reg2, reg1, Offset.zero().plus(4));	\\ Argument 1 to be passed stored in EAX
	emitMOV_Reg_RegInd(reg4, reg1);			\\ Argument 2 stored in EDX
	emitPUSH_Imm(VM.block_index);			\\ Argument 3 pushed on stack(Block index, associated with local code block)
	emitPUSH_Imm(VM.ins_index);			\\ Argument 4 pushed on stack(Offset within block)
	emitCALL_Abs(VM.address_var);			\\ Making a call to function inside Jikes, to print the 4 arguments. The function is 
							\\ my_check_function() in file RuntimeEntryPoints.java
	setMachineCodes(mi++, (byte) 0x9D);		\\ Restoring Flags
	emitPOP_Reg(reg5);				\\ Restoring registers
	emitPOP_Reg(reg4);
	emitPOP_Reg(reg2);
	instrumented_code = true; 			\\ Turning the instrumentation On
	VM.ins_index++;					\\ Incrementing the variable associated with offset of instruction within code block
	
     }

  }

------------------------------------------------------------


2. public final void instrument_add (GPR reg, short scale, Offset disp, int type, int flagg)

This function is again added in file assembler.java(changes made in file assembler.in). This function adds a sequence of machine instructions in the machine code array. This sequence of instructions will print the address accessed by the program. Depending upon the mode of addressing, i.e. direct, indirect etc various cases are handled. This function is similar to the above function.

-----------------------------------------------------------

3. public final void address_ins(GPR srcBase, GPR srcIndex, short srcScale, Offset srcDisp, int Address, int type, int flagg)

This function is again added in file assembler.java(changes made in file assembler.in). This function adds a sequence of machine instructions in the machine code array. This sequence of instructions will print the address accessed by the program. Depending upon the mode of addressing, i.e. direct, indirect etc various cases are handled. This function is similar to the above function.

-----------------------------------------------------------

4. my_check_function(): This function is declared in file RuntimeEntryPoints.java. This is the function being called by the sequence of
instruction inserted by the function instrument_fun(). This will simply print the arguments it receives on terminal.

-----------------------------------------------------------

Similarly, my_add_load() and my_add_store() are two functions which are declared there and print the values passed to them.

----------------------------------------------------------------------------------------------

Description of functions added

-----------------------------------------------------------

1. forwardJcc (byte cond)	(Assembler.java)

This function originally used a Short Branch, as it assumed the offset will fit into 8 bits. However, when the instrumentation code is inserted
offset becomes large, so the function was changed to use a Conditional Branch instead when the trace is being collected, and a short branch in other cases.
 
-----------------------------------------------------------
For documentation of postprocessing steps, refer to README file inside the folder disas
