Generating a class file for a small programming language

hello

Im in the middle of writing code to generate a java class file containing the bytecode etc from the result of compiling a small programming language I created for a project, with the intent on getting the JVM to execute the generated class file. I've already created all the necessary data structures to store the data for the class file (constant pool etc).

My question is, when storing the op - code of a byte code instruction in the code[] array contained in a code attribute for a method, how / where do you also store the value of the operand(s)? E.G when loading an int value from some index (say 21), the byte code mnemonic is iload and the operand is 21, so I store the opcode hex value (15) in the code[] array (I think you store the instructions op code in the code array, if im wrong please tell me) but I'm not sure how I'd store the operand (21).

thanks in advance (also any other pointers on class file generation would be appreciated)

adam

[995 byte] By [adamcuza] at [2007-11-26 19:52:31]
# 1

In the case if iload,

http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.doc6.html#iload

you store the operand 21, immediately after your opcode 15, in the code array as an unsigned byte.

When I'm reading / disassembling the iload instruction, I first read the

opcode ie.

int opCode = bytecodeStream.readUnsignedByte();

then, in my specific op21 class ( iload ), I read the operand like this

if ( isWide() )

{

variableTableEntry = dissassembler.getByteCodeStream().readUnsignedShort();

}

else

{

variableTableEntry = dissassembler.getByteCodeStream().readUnsignedByte();

}

This is because iload "can" potentially be modified by a WIDE prefix.

In your simple examples you won't use wide.

So you can write your operands as unsigned bytes.

Some opcodes take parameters which are popped from the stack.

In which case they parameters must be pushed on to the stack in correct order, before invoking the opcode.

regards,

Owen

omcgoverna at 2007-7-9 22:43:24 > top of Java-index,Developer Tools,Java Compiler...