Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members | Related Pages

bytecode.hpp

00001 /*
00002  * Copyright (c) 2003, Raymond Bosman
00003  * Copyright (c) 2003, Frederik Holljen
00004  * All Rights Reserved.
00005  *
00006  * See COPYING for licensing.
00007  */
00008 
00009 
00010 #ifndef BYTECODE_HPP
00011 #define BYTECODE_HPP
00012 
00013 #include <fstream>
00014 #include <vector>
00015 
00016 #include "defs.hpp"
00017 #include "classwriterbase.hpp"
00018 
00019 namespace ClassEncoder
00020 {
00021 
00022 class ConstantPool;
00023 
00025 
00030 class ByteCode : public ClassWriterBase
00031 {
00032 public:
00033     ByteCode( ConstantPool *pool );
00034     virtual ~ByteCode();
00035 
00037 
00040     unsigned int maxStackSize() const { return MaxStackSize; }
00042 
00045     unsigned int nextInstrAddr() const { return Code.size(); }
00046 
00047     void write( std::ofstream &stream ) const;
00048 
00049     // EMIT FUNCTIONS
00050     // Only the fuctions we need are implemented here..
00051     // see http://www.comnets.rwth-aachen.de/doc/java/vmspec/vmspec-12.html#HEADING12-0 for complete listings of all functions in each category
00052     // PUSH CONSTANTS ONTO THE STACK
00053     unsigned int emit_iconst( int number );
00054     unsigned int emit_bipush( u1 num );
00055     unsigned int emit_sipush( u2 num );
00056     unsigned int emit_ldc( u1 index );
00057     unsigned int emit_ldc2( u2 index );
00058 
00059     // LOADING LOCALS ON THE STACK
00060     unsigned int emit_iload( u1 vindex );
00061     unsigned int emit_lload( u1 vindex );
00062     unsigned int emit_fload( u1 vindex );
00063     unsigned int emit_dload( u1 vindex );
00064     unsigned int emit_aload( u1 vindex );
00065 
00066     // STORING STACK TO LOCALS
00067     unsigned int emit_istore( u1 vindex );
00068     unsigned int emit_lstore( u1 vindex );
00069     unsigned int emit_fstore( u1 vindex );
00070     unsigned int emit_dstore( u1 vindex );
00071     unsigned int emit_astore( u1 vindex );
00072     unsigned int emit_iinc( u1 vindex, u1 value ); // increment integer var at vindex with value
00073 
00074     // ARRAY FUNCTIONS
00075     unsigned int emit_newarray( _basic_data_types type );
00076     unsigned int emit_multianewarray( u2 index, u1 dimensions );
00077     unsigned int emit_multianewarray( const std::string classType, u1 dimensions );
00078 
00079     unsigned int emit_iastore();
00080     unsigned int emit_lastore();
00081     unsigned int emit_fastore();
00082     unsigned int emit_dastore();
00083     unsigned int emit_aastore();
00084     unsigned int emit_bastore();
00085     unsigned int emit_castore();
00086     unsigned int emit_sastore();
00087 
00088     unsigned int emit_iaload();
00089     unsigned int emit_laload();
00090     unsigned int emit_faload();
00091     unsigned int emit_daload();
00092     unsigned int emit_aaload();
00093     unsigned int emit_baload();
00094     unsigned int emit_caload();
00095     unsigned int emit_saload();
00096 
00097     // STACK INSTRUCTIONS
00098     unsigned int emit_pop();
00099     unsigned int emit_pop2();
00100     unsigned int emit_dup();
00101     unsigned int emit_dup2();
00102     unsigned int emit_dup_x1();
00103     unsigned int emit_dup_x2();
00104     unsigned int emit_swap();
00105 
00106     // ARITHMETIC INSTRUCTIONS
00107 private:
00108     unsigned int emit_arithmetic( _opcode code, unsigned int popVal = 1 );
00109 public:
00110     unsigned int emit_iadd(); // add
00111     unsigned int emit_ladd();
00112     unsigned int emit_fadd();
00113     unsigned int emit_dadd();
00114     unsigned int emit_isub(); // sub
00115     unsigned int emit_lsub();
00116     unsigned int emit_fsub();
00117     unsigned int emit_dsub();
00118     unsigned int emit_imul(); // mul
00119     unsigned int emit_lmul();
00120     unsigned int emit_fmul();
00121     unsigned int emit_dmul();
00122     unsigned int emit_idiv(); // div
00123     unsigned int emit_ldiv();
00124     unsigned int emit_fdiv();
00125     unsigned int emit_ddiv();
00126     unsigned int emit_irem(); // mod
00127     unsigned int emit_lrem();
00128     unsigned int emit_frem();
00129     unsigned int emit_drem();
00130     unsigned int emit_ineg(); // invert
00131     unsigned int emit_lneg();
00132     unsigned int emit_fneg();
00133     unsigned int emit_dneg();
00134 
00135     // LOGICAL INSTRUCTIONS
00136     unsigned int emit_iand();
00137     unsigned int emit_ior();
00138     unsigned int emit_ixor();
00139 
00140     // CONVERSION OPERATIONS
00141 
00142     // CONTROL TRANSFER
00143 private:
00144     unsigned int emit_transfer( _opcode code, u2 jump, unsigned int popVal = 1 );
00145 public:
00146     // patch jump
00147     void patch( unsigned int instrAddr, u2 jump );
00148 
00149     unsigned int emit_ifeq( u2 jump );
00150     unsigned int emit_ifnull( u2 jump );
00151     unsigned int emit_iflt( u2 jump ); // less than
00152     unsigned int emit_ifle( u2 jump ); // less or equal
00153     unsigned int emit_ifne( u2 jump ); // not equal
00154     unsigned int emit_ifnonnull( u2 jump );
00155     unsigned int emit_ifgt( u2 jump ); // greater than
00156     unsigned int emit_ifge( u2 jump ); // greater or equal
00157     unsigned int emit_if_icmpeq( u2 jump ); // top two stack integers equal
00158     unsigned int emit_if_icmpne( u2 jump ); // top two stack integers not equal
00159     unsigned int emit_if_icmplt( u2 jump ); // integer stack 2 less than stack 1
00160     unsigned int emit_if_icmpgt( u2 jump ); // integer stack 2 greater than stack 1
00161     unsigned int emit_if_icmple( u2 jump ); // stack2 less or equal stack 1
00162     unsigned int emit_if_icmpge( u2 jump ); // stack 2 greater or equal stack 1
00163     unsigned int emit_goto( u2 jump );
00164     unsigned int emit_jsr( u2 jump ); // jump subroutine (ret on stack)
00165     unsigned int emit_ret( u1 vindex );
00166 
00167     // MANIPULATING OBJECT FIELDS
00168     unsigned int emit_getstatic( u2 index );
00169     unsigned int emit_getstatic( const std::string &className, const std::string &fieldName,
00170                                  const std::string &fieldType );
00171 
00172     // TABLE JUMPING (for switch)
00173 
00174     // METHOD INVOCATION
00175     /* TODO: Add num params, so we can pop correctly */
00176     unsigned int emit_invokevirtual( u2 index );
00177     unsigned int emit_invokespecial( u2 index );
00178     unsigned int emit_invokestatic( u2 index );
00179     unsigned int emit_invokeinterface( u2 index, u1 nargs );
00180 
00181     unsigned int emit_invokestatic( const std::string &classType, const std::string &function,
00182                                     const std::string &footprint );
00183     unsigned int emit_invokevirtual( const std::string &classType, const std::string &function,
00184                                      const std::string &footprint );
00185     unsigned int emit_invokespecial( const std::string &object, const std::string &function,
00186                                      const std::string &footprint );
00187     unsigned int emit_invokeinterface( const std::string &objectType, const std::string &function,
00188                                        const std::string &footprint, u1 nargs );
00189     // MISC OBJECT FUNCTIONS
00190     unsigned int emit_new( u2 index );
00191     unsigned int emit_new( const std::string &className );
00192 
00193     // FUNCTION RETURN
00194     unsigned int emit_ireturn();
00195     unsigned int emit_lreturn();
00196     unsigned int emit_freturn();
00197     unsigned int emit_dreturn();
00198     unsigned int emit_return();
00199 
00200 private:
00201     void push( int num = 1 );
00202     void pop( int num = 1 );
00203 
00204 private:
00205     unsigned int MaxStackSize;
00206     unsigned int StackSize;
00207     std::vector<u1> Code;
00208 
00209     ConstantPool *ConstantPoolEntries;
00210 };
00211 
00212 } // end of namespace
00213 #endif // BYTECODE_HPP

Generated on Mon Dec 1 14:26:27 2003 for Ck by doxygen 1.3.3