/// <exception cref="Kirikiri.Tjs2.TJSException"></exception> private void ExportByteCode(BinaryStream output) { byte[] filetag = FILE_TAG; byte[] codetag = new byte[] { (byte)('T'), (byte)('J'), (byte)('S'), (byte)('2') }; byte[] objtag = new byte[] { (byte)('O'), (byte)('B'), (byte)('J'), (byte)('S') }; byte[] datatag = new byte[] { (byte)('D'), (byte)('A'), (byte)('T'), (byte)('A') }; int count = mInterCodeGeneratorList.Count; AList<ByteBuffer> objarray = new AList<ByteBuffer>(count * 2); ConstArrayData constarray = new ConstArrayData(); int objsize = 0; for (int i = 0; i < count; i++) { InterCodeGenerator obj = mInterCodeGeneratorList[i]; ByteBuffer buf = obj.ExportByteCode(this, constarray); objarray.AddItem(buf); objsize += buf.Capacity() + TAG_SIZE + CHUNK_SIZE_LEN; } // tag + size objsize += TAG_SIZE + CHUNK_SIZE_LEN + 4 + 4; // OBJS tag + size + toplevel + count ByteBuffer dataarea = constarray.ExportBuffer(); int datasize = dataarea.Capacity() + TAG_SIZE + CHUNK_SIZE_LEN; // DATA tag + size int filesize = objsize + datasize + FILE_TAG_SIZE + CHUNK_SIZE_LEN; // TJS2 tag + file size byte[] filesizearray = new byte[] { unchecked((byte)(filesize & unchecked((int)(0xff )))), unchecked((byte)(((int)(((uint)filesize) >> 8)) & unchecked((int)(0xff)))) , unchecked((byte)(((int)(((uint)filesize) >> 16)) & unchecked((int)(0xff)))), unchecked( (byte)(((int)(((uint)filesize) >> 24)) & unchecked((int)(0xff)))) }; byte[] datasizearray = new byte[] { unchecked((byte)(datasize & unchecked((int)(0xff )))), unchecked((byte)(((int)(((uint)datasize) >> 8)) & unchecked((int)(0xff)))) , unchecked((byte)(((int)(((uint)datasize) >> 16)) & unchecked((int)(0xff)))), unchecked( (byte)(((int)(((uint)datasize) >> 24)) & unchecked((int)(0xff)))) }; byte[] objsizearray = new byte[] { unchecked((byte)(objsize & unchecked((int)(0xff )))), unchecked((byte)(((int)(((uint)objsize) >> 8)) & unchecked((int)(0xff)))), unchecked((byte)(((int)(((uint)objsize) >> 16)) & unchecked((int)(0xff)))), unchecked( (byte)(((int)(((uint)objsize) >> 24)) & unchecked((int)(0xff)))) }; byte[] objcountarray = new byte[] { unchecked((byte)(count & unchecked((int)(0xff )))), unchecked((byte)(((int)(((uint)count) >> 8)) & unchecked((int)(0xff)))), unchecked( (byte)(((int)(((uint)count) >> 16)) & unchecked((int)(0xff)))), unchecked((byte) (((int)(((uint)count) >> 24)) & unchecked((int)(0xff)))) }; int toplevel = -1; if (mTopLevelGenerator != null) { toplevel = GetCodeIndex(mTopLevelGenerator); } byte[] toparray = new byte[] { unchecked((byte)(toplevel & unchecked((int)(0xff)) )), unchecked((byte)(((int)(((uint)toplevel) >> 8)) & unchecked((int)(0xff)))), unchecked((byte)(((int)(((uint)toplevel) >> 16)) & unchecked((int)(0xff)))), unchecked( (byte)(((int)(((uint)toplevel) >> 24)) & unchecked((int)(0xff)))) }; output.Write(filetag); output.Write(filesizearray); output.Write(datatag); output.Write(datasizearray); output.Write(dataarea); output.Write(objtag); output.Write(objsizearray); output.Write(toparray); output.Write(objcountarray); for (int i_1 = 0; i_1 < count; i_1++) { ByteBuffer buf = objarray[i_1]; int size = buf.Capacity(); byte[] bufsizearray = new byte[] { unchecked((byte)(size & unchecked((int)(0xff)) )), unchecked((byte)(((int)(((uint)size) >> 8)) & unchecked((int)(0xff)))), unchecked( (byte)(((int)(((uint)size) >> 16)) & unchecked((int)(0xff)))), unchecked((byte)( ((int)(((uint)size) >> 24)) & unchecked((int)(0xff)))) }; output.Write(codetag); output.Write(bufsizearray); output.Write(buf); } output.Close(); output = null; objarray.Clear(); objarray = null; constarray = null; dataarea = null; }
// VMCodes // SubType // FuncArgType // result needed // condition flag needed // true logic // inverted logic /// <summary>バイトコードを出力する</summary> /// <returns></returns> public virtual ByteBuffer ExportByteCode(Compiler block, ConstArrayData constarray ) { int parent = -1; if (mParent != null) { parent = block.GetCodeIndex(mParent); } int propSetter = -1; if (mPropSetter != null) { propSetter = block.GetCodeIndex(mPropSetter); } int propGetter = -1; if (mPropGetter != null) { propGetter = block.GetCodeIndex(mPropGetter); } int superClassGetter = -1; if (mSuperClassGetter != null) { superClassGetter = block.GetCodeIndex(mSuperClassGetter); } int name = -1; if (mName != null) { name = constarray.PutString(mName); } // 13 * 4 データ部分のサイズ //int srcpossize = mSourcePosArray != null ? mSourcePosArray.position() * 8 : 0; // mSourcePosArray は配列の方がいいかな, codepos, sorcepos の顺で、int型で登录した方がいいかも int srcpossize = 0; // 主にデバッグ用の情报なので出力抑止 int codesize = (mCode.Length % 2) == 1 ? mCode.Length * 2 + 2 : mCode.Length * 2; int datasize = mDataArray.Length * 4; int scgpsize = mSuperClassGetterPointer != null ? mSuperClassGetterPointer.Size() * 4 : 0; int propsize = (mProperties != null ? mProperties.Count * 8 : 0) + 4; int size = 12 * 4 + srcpossize + codesize + datasize + scgpsize + propsize + 4 * 4; ByteBuffer result = ByteBuffer.Allocate(size); result.Order(ByteOrder.LITTLE_ENDIAN); result.Clear(); IntBuffer buf = result.AsIntBuffer(); buf.Clear(); buf.Put(parent); buf.Put(name); buf.Put(mContextType); buf.Put(mMaxVariableCount); buf.Put(mVariableReserveCount); buf.Put(mMaxFrameCount); buf.Put(mFuncDeclArgCount); buf.Put(mFuncDeclUnnamedArgArrayBase); buf.Put(mFuncDeclCollapseBase); buf.Put(propSetter); buf.Put(propGetter); buf.Put(superClassGetter); //int count = mSourcePosArray != null ? mSourcePosArray.position() : 0; int count = 0; buf.Put(count); // 主にデバッグ用の情报なので出力抑止 count = mCode.Length; buf.Put(count); ShortBuffer sbuf = result.AsShortBuffer(); sbuf.Clear(); sbuf.Position(buf.Position() * 2); sbuf.Put(mCode); if ((count % 2) == 1) { // アライメント sbuf.Put((short)0); } buf.Position(sbuf.Position() / 2); count = mDataArray.Length; buf.Put(count); sbuf.Position(buf.Position() * 2); for (int i = 0; i < count; i++) { Variant val = mDataArray[i]; short type = constarray.GetType(val); short v = (short)constarray.PutVariant(val, block); sbuf.Put(type); sbuf.Put(v); } buf.Position(sbuf.Position() / 2); count = mSuperClassGetterPointer != null ? mSuperClassGetterPointer.Size() : 0; buf.Put(count); for (int i_1 = 0; i_1 < count; i_1++) { int v = mSuperClassGetterPointer.Get(i_1); buf.Put(v); } count = 0; if (mProperties != null) { count = mProperties.Count; buf.Put(count); if (count > 0) { for (int i_2 = 0; i_2 < count; i_2++) { InterCodeGenerator.Property prop = mProperties[i_2]; // .Value.dumpClassStructure(nest+1); int propname = constarray.PutString(prop.Name); int propobj = -1; if (prop.Value != null) { propobj = block.GetCodeIndex(prop.Value); } buf.Put(propname); buf.Put(propobj); } } } else { buf.Put(count); } result.Limit(result.Capacity()); result.Position(0); return result; }