mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-25 07:27:52 +01:00
add support for long const
This commit is contained in:
parent
2766f8170a
commit
92a878b5bf
@ -93,16 +93,16 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
|||||||
WasmOutputStream stream = new WasmOutputStream();
|
WasmOutputStream stream = new WasmOutputStream();
|
||||||
stream.writeVaruint32( count );
|
stream.writeVaruint32( count );
|
||||||
for( FunctionType type : functionTypes ) {
|
for( FunctionType type : functionTypes ) {
|
||||||
stream.writeVarint32( ValueType.func.getCode() );
|
stream.writeVarint( ValueType.func.getCode() );
|
||||||
stream.writeVaruint32( type.params.size() );
|
stream.writeVaruint32( type.params.size() );
|
||||||
for( ValueType valueType : type.params ) {
|
for( ValueType valueType : type.params ) {
|
||||||
stream.writeVarint32( valueType.getCode() );
|
stream.writeVarint( valueType.getCode() );
|
||||||
}
|
}
|
||||||
if( type.result == null ) {
|
if( type.result == null ) {
|
||||||
stream.writeVaruint32( 0 );
|
stream.writeVaruint32( 0 );
|
||||||
} else {
|
} else {
|
||||||
stream.writeVaruint32( 1 );
|
stream.writeVaruint32( 1 );
|
||||||
stream.writeVarint32( type.result.getCode() );
|
stream.writeVarint( type.result.getCode() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
wasm.writeSection( SectionType.Type, stream, null );
|
wasm.writeSection( SectionType.Type, stream, null );
|
||||||
@ -217,7 +217,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
|||||||
localsStream.writeVaruint32( locals.size() );
|
localsStream.writeVaruint32( locals.size() );
|
||||||
for( ValueType valueType : locals ) {
|
for( ValueType valueType : locals ) {
|
||||||
localsStream.writeVaruint32( 1 ); // TODO optimize, write the count of same types.
|
localsStream.writeVaruint32( 1 ); // TODO optimize, write the count of same types.
|
||||||
localsStream.writeVarint32( valueType.getCode() );
|
localsStream.writeVarint( valueType.getCode() );
|
||||||
}
|
}
|
||||||
functionsStream.writeVaruint32( localsStream.size() + codeStream.size() + 1 );
|
functionsStream.writeVaruint32( localsStream.size() + codeStream.size() + 1 );
|
||||||
localsStream.writeTo( functionsStream );
|
localsStream.writeTo( functionsStream );
|
||||||
@ -231,7 +231,16 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
|||||||
@Override
|
@Override
|
||||||
protected void writeConstInt( int value ) throws IOException {
|
protected void writeConstInt( int value ) throws IOException {
|
||||||
codeStream.write( I32_CONST );
|
codeStream.write( I32_CONST );
|
||||||
codeStream.writeVarint32( value );
|
codeStream.writeVarint( value );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void writeConstLong( long value ) throws IOException {
|
||||||
|
codeStream.write( I64_CONST );
|
||||||
|
codeStream.writeVarint( value );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,166 +1,166 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2017 Volker Berlin (i-net software)
|
* Copyright 2017 Volker Berlin (i-net software)
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package de.inetsoftware.jwebassembly.binary;
|
package de.inetsoftware.jwebassembly.binary;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.FilterOutputStream;
|
import java.io.FilterOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
import javax.annotation.Nonnegative;
|
import javax.annotation.Nonnegative;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Volker Berlin
|
* @author Volker Berlin
|
||||||
*/
|
*/
|
||||||
class WasmOutputStream extends FilterOutputStream {
|
class WasmOutputStream extends FilterOutputStream {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a in memory stream.
|
* Create a in memory stream.
|
||||||
*/
|
*/
|
||||||
WasmOutputStream() {
|
WasmOutputStream() {
|
||||||
super( new ByteArrayOutputStream() );
|
super( new ByteArrayOutputStream() );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a wrapped stream.
|
* Create a wrapped stream.
|
||||||
*
|
*
|
||||||
* @param output
|
* @param output
|
||||||
* the target of data
|
* the target of data
|
||||||
*/
|
*/
|
||||||
WasmOutputStream( OutputStream output ) {
|
WasmOutputStream( OutputStream output ) {
|
||||||
super( output );
|
super( output );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write a integer little endian (ever 4 bytes)
|
* Write a integer little endian (ever 4 bytes)
|
||||||
*
|
*
|
||||||
* @param value
|
* @param value
|
||||||
* the value
|
* the value
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* if an I/O error occurs.
|
* if an I/O error occurs.
|
||||||
*/
|
*/
|
||||||
void writeInt32( int value ) throws IOException {
|
void writeInt32( int value ) throws IOException {
|
||||||
write( (value >>> 0) & 0xFF );
|
write( (value >>> 0) & 0xFF );
|
||||||
write( (value >>> 8) & 0xFF );
|
write( (value >>> 8) & 0xFF );
|
||||||
write( (value >>> 16) & 0xFF );
|
write( (value >>> 16) & 0xFF );
|
||||||
write( (value >>> 24) & 0xFF );
|
write( (value >>> 24) & 0xFF );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write an unsigned integer.
|
* Write an unsigned integer.
|
||||||
*
|
*
|
||||||
* @param value
|
* @param value
|
||||||
* the value
|
* the value
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* if an I/O error occurs.
|
* if an I/O error occurs.
|
||||||
*/
|
*/
|
||||||
void writeVaruint32( @Nonnegative int value ) throws IOException {
|
void writeVaruint32( @Nonnegative int value ) throws IOException {
|
||||||
if( value < 0 ) {
|
if( value < 0 ) {
|
||||||
throw new IOException( "Invalid negative value" );
|
throw new IOException( "Invalid negative value" );
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
int b = value & 0x7F; // low 7 bits
|
int b = value & 0x7F; // low 7 bits
|
||||||
value >>= 7;
|
value >>= 7;
|
||||||
if( value != 0 ) { /* more bytes to come */
|
if( value != 0 ) { /* more bytes to come */
|
||||||
b |= 0x80;
|
b |= 0x80;
|
||||||
}
|
}
|
||||||
write( b );
|
write( b );
|
||||||
} while( value != 0 );
|
} while( value != 0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write an integer value.
|
* Write an integer value.
|
||||||
*
|
*
|
||||||
* @param value
|
* @param value
|
||||||
* the value
|
* the value
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* if an I/O error occurs.
|
* if an I/O error occurs.
|
||||||
*/
|
*/
|
||||||
void writeVarint32( int value ) throws IOException {
|
void writeVarint( long value ) throws IOException {
|
||||||
while( true ) {
|
while( true ) {
|
||||||
int b = value & 0x7F;
|
int b = (int)value & 0x7F;
|
||||||
value >>= 7;
|
value >>= 7;
|
||||||
|
|
||||||
/* sign bit of byte is second high order bit (0x40) */
|
/* sign bit of byte is second high order bit (0x40) */
|
||||||
if( (value == 0 && (b & 0x40) == 0) || (value == -1 && (b & 0x40) != 0) ) {
|
if( (value == 0 && (b & 0x40) == 0) || (value == -1 && (b & 0x40) != 0) ) {
|
||||||
write( b );
|
write( b );
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
write( b | 0x80 );
|
write( b | 0x80 );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write a section with header and data.
|
* Write a section with header and data.
|
||||||
*
|
*
|
||||||
* @param type
|
* @param type
|
||||||
* the name of the section
|
* the name of the section
|
||||||
* @param data
|
* @param data
|
||||||
* the data of the section
|
* the data of the section
|
||||||
* @param name
|
* @param name
|
||||||
* the name, must be set if the id == 0
|
* the name, must be set if the id == 0
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* if any I/O error occur
|
* if any I/O error occur
|
||||||
*/
|
*/
|
||||||
void writeSection( SectionType type, WasmOutputStream data, String name ) throws IOException {
|
void writeSection( SectionType type, WasmOutputStream data, String name ) throws IOException {
|
||||||
ByteArrayOutputStream baos = (ByteArrayOutputStream)data.out;
|
ByteArrayOutputStream baos = (ByteArrayOutputStream)data.out;
|
||||||
int size = baos.size();
|
int size = baos.size();
|
||||||
if( size == 0 ) {
|
if( size == 0 ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
writeVaruint32( type.ordinal() );
|
writeVaruint32( type.ordinal() );
|
||||||
writeVaruint32( size );
|
writeVaruint32( size );
|
||||||
if( type == SectionType.Custom ) {
|
if( type == SectionType.Custom ) {
|
||||||
byte[] bytes = name.getBytes( StandardCharsets.ISO_8859_1 );
|
byte[] bytes = name.getBytes( StandardCharsets.ISO_8859_1 );
|
||||||
writeVaruint32( bytes.length );
|
writeVaruint32( bytes.length );
|
||||||
write( bytes );
|
write( bytes );
|
||||||
}
|
}
|
||||||
baos.writeTo( this );
|
baos.writeTo( this );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write the data of this stream to the output. Work only for in memory stream.
|
* Write the data of this stream to the output. Work only for in memory stream.
|
||||||
*
|
*
|
||||||
* @param output
|
* @param output
|
||||||
* the target
|
* the target
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* if any I/O error occur
|
* if any I/O error occur
|
||||||
*/
|
*/
|
||||||
void writeTo( OutputStream output ) throws IOException {
|
void writeTo( OutputStream output ) throws IOException {
|
||||||
ByteArrayOutputStream baos = (ByteArrayOutputStream)out;
|
ByteArrayOutputStream baos = (ByteArrayOutputStream)out;
|
||||||
baos.writeTo( output );
|
baos.writeTo( output );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The count of bytes in the stream. Work only for in memory stream.
|
* The count of bytes in the stream. Work only for in memory stream.
|
||||||
*
|
*
|
||||||
* @return the data size
|
* @return the data size
|
||||||
*/
|
*/
|
||||||
int size() {
|
int size() {
|
||||||
ByteArrayOutputStream baos = (ByteArrayOutputStream)out;
|
ByteArrayOutputStream baos = (ByteArrayOutputStream)out;
|
||||||
return baos.size();
|
return baos.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reset the stream. Work only for in memory stream.
|
* Reset the stream. Work only for in memory stream.
|
||||||
*/
|
*/
|
||||||
void reset() {
|
void reset() {
|
||||||
ByteArrayOutputStream baos = (ByteArrayOutputStream)out;
|
ByteArrayOutputStream baos = (ByteArrayOutputStream)out;
|
||||||
baos.reset();
|
baos.reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,7 @@ import de.inetsoftware.classparser.Annotations;
|
|||||||
import de.inetsoftware.classparser.ClassFile;
|
import de.inetsoftware.classparser.ClassFile;
|
||||||
import de.inetsoftware.classparser.Code;
|
import de.inetsoftware.classparser.Code;
|
||||||
import de.inetsoftware.classparser.CodeInputStream;
|
import de.inetsoftware.classparser.CodeInputStream;
|
||||||
|
import de.inetsoftware.classparser.ConstantPool;
|
||||||
import de.inetsoftware.classparser.LineNumberTable;
|
import de.inetsoftware.classparser.LineNumberTable;
|
||||||
import de.inetsoftware.classparser.MethodInfo;
|
import de.inetsoftware.classparser.MethodInfo;
|
||||||
import de.inetsoftware.jwebassembly.WasmException;
|
import de.inetsoftware.jwebassembly.WasmException;
|
||||||
@ -93,11 +94,11 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
i + 1 == lineNumberTable.size() ? code.getCodeSize()
|
i + 1 == lineNumberTable.size() ? code.getCodeSize()
|
||||||
: lineNumberTable.getStartOffset( i + 1 );
|
: lineNumberTable.getStartOffset( i + 1 );
|
||||||
CodeInputStream byteCode = code.getByteCode( offset, nextOffset - offset );
|
CodeInputStream byteCode = code.getByteCode( offset, nextOffset - offset );
|
||||||
writeCodeChunk( byteCode, lineNumber );
|
writeCodeChunk( byteCode, lineNumber, method.getConstantPool() );
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
CodeInputStream byteCode = code.getByteCode();
|
CodeInputStream byteCode = code.getByteCode();
|
||||||
writeCodeChunk( byteCode, -1 );
|
writeCodeChunk( byteCode, -1, method.getConstantPool() );
|
||||||
}
|
}
|
||||||
for( int i = Math.min( paramCount, locals.size() ); i > 0; i-- ) {
|
for( int i = Math.min( paramCount, locals.size() ); i > 0; i-- ) {
|
||||||
locals.remove( 0 );
|
locals.remove( 0 );
|
||||||
@ -237,7 +238,7 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
* @throws WasmException
|
* @throws WasmException
|
||||||
* if some Java code can't converted
|
* if some Java code can't converted
|
||||||
*/
|
*/
|
||||||
private void writeCodeChunk( CodeInputStream byteCode, int lineNumber ) throws WasmException {
|
private void writeCodeChunk( CodeInputStream byteCode, int lineNumber, ConstantPool constantPool ) throws WasmException {
|
||||||
try {
|
try {
|
||||||
while( byteCode.available() > 0 ) {
|
while( byteCode.available() > 0 ) {
|
||||||
int op = byteCode.readUnsignedByte();
|
int op = byteCode.readUnsignedByte();
|
||||||
@ -248,6 +249,9 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
case 16: //bipush
|
case 16: //bipush
|
||||||
writeConstInt( byteCode.readByte() );
|
writeConstInt( byteCode.readByte() );
|
||||||
break;
|
break;
|
||||||
|
case 20: //ldc2_w
|
||||||
|
writeConstLong( (Long)constantPool.get( byteCode.readUnsignedShort() ) );
|
||||||
|
break;
|
||||||
case 26: // iload_0
|
case 26: // iload_0
|
||||||
case 27: // iload_1
|
case 27: // iload_1
|
||||||
case 28: // iload_2
|
case 28: // iload_2
|
||||||
@ -264,6 +268,7 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
writeAddInt();
|
writeAddInt();
|
||||||
break;
|
break;
|
||||||
case 172: // ireturn
|
case 172: // ireturn
|
||||||
|
case 173: // lreturn
|
||||||
case 177: // return void
|
case 177: // return void
|
||||||
writeReturn();
|
writeReturn();
|
||||||
break;
|
break;
|
||||||
@ -286,6 +291,30 @@ public abstract class ModuleWriter implements Closeable {
|
|||||||
*/
|
*/
|
||||||
protected abstract void writeConstInt( int value ) throws IOException;
|
protected abstract void writeConstInt( int value ) throws IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a constant long value
|
||||||
|
*
|
||||||
|
* @param value
|
||||||
|
* the value
|
||||||
|
* @throws IOException
|
||||||
|
* if any I/O error occur
|
||||||
|
*/
|
||||||
|
protected abstract void writeConstLong( long value ) throws IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write or Load a local variable.
|
||||||
|
*
|
||||||
|
* @param load
|
||||||
|
* true: if load
|
||||||
|
* @param valueType
|
||||||
|
* the type of the variable
|
||||||
|
* @param idx
|
||||||
|
* the idx of the variable
|
||||||
|
* @throws WasmException
|
||||||
|
* occur a if a variable was used for a different type
|
||||||
|
* @throws IOException
|
||||||
|
* if any I/O error occur
|
||||||
|
*/
|
||||||
private void writeLoadStore( boolean load, @Nonnull ValueType valueType, @Nonnegative int idx ) throws WasmException, IOException {
|
private void writeLoadStore( boolean load, @Nonnull ValueType valueType, @Nonnegative int idx ) throws WasmException, IOException {
|
||||||
while( locals.size() <= idx ) {
|
while( locals.size() <= idx ) {
|
||||||
locals.add( null );
|
locals.add( null );
|
||||||
|
@ -111,6 +111,15 @@ public class TextModuleWriter extends ModuleWriter {
|
|||||||
methodOutput.append( "i32.const " ).append( Integer.toString( value ) );
|
methodOutput.append( "i32.const " ).append( Integer.toString( value ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void writeConstLong( long value ) throws IOException {
|
||||||
|
newline( methodOutput );
|
||||||
|
methodOutput.append( "i64.const " ).append( Long.toString( value ) );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user