add TypeManager

This commit is contained in:
Volker Berlin 2018-12-19 20:10:26 +01:00
parent 6196648cc0
commit bfc6cbb3f0
4 changed files with 95 additions and 3 deletions

View File

@ -26,6 +26,7 @@ import de.inetsoftware.classparser.ConstantClass;
import de.inetsoftware.classparser.ConstantPool;
import de.inetsoftware.classparser.ConstantRef;
import de.inetsoftware.jwebassembly.WasmException;
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
import de.inetsoftware.jwebassembly.wasm.NumericOperator;
import de.inetsoftware.jwebassembly.wasm.StorageType;
@ -545,7 +546,7 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder {
//TODO case 186: // invokedynamic
case 187: // new
String name = ((ConstantClass)constantPool.get( byteCode.readUnsignedShort() )).getName();
StorageType storageType = ValueType.anyref;
StorageType storageType = new StructType(name);
addStructInstruction( StructOperator.NEW_DEFAULT, storageType, codePos );
break;
case 188: // newarray

View File

@ -58,6 +58,8 @@ public class ModuleGenerator {
private FunctionManager functions = new FunctionManager();
private TypeManager types = new TypeManager();
/**
* Create a new generator.
*
@ -237,6 +239,8 @@ public class ModuleGenerator {
case Call:
functions.functionCall( ((WasmCallInstruction)instruction).getFunctionName() );
break;
case Struct:
types.useType( ((WasmStructInstruction)instruction).getStorageType() );
default:
}
instruction.writeTo( writer );

View File

@ -0,0 +1,80 @@
/*
Copyright 2018 Volker Berlin (i-net software)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package de.inetsoftware.jwebassembly.module;
import java.util.HashMap;
import de.inetsoftware.jwebassembly.wasm.StorageType;
/**
* Manage the written and to write types (classes)
*
* @author Volker Berlin
*/
class TypeManager {
private final HashMap<String, StructType> map = new HashMap<>();
/**
* Use the type in the output.
*
* @param storageType
* the reference to a type
*/
void useType( StorageType storageType ) {
if( storageType instanceof StructType ) {
StructType type = (StructType)storageType;
StructType existingType = map.get( type.name );
if( existingType == null ) {
type.code = map.size();
map.put( type.name, type );
} else {
type.code = existingType.code;
}
}
}
/**
* A reference to a type.
*
* @author Volker Berlin
*/
static class StructType implements StorageType {
private final String name;
private int code;
/**
* Create a reference to type
*
* @param name
* the Java class name
*/
StructType( String name ) {
this.name = name;
}
/**
* {@inheritDoc}
*/
@Override
public int getCode() {
return code;
}
}
}

View File

@ -22,8 +22,6 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import de.inetsoftware.jwebassembly.WasmException;
import de.inetsoftware.jwebassembly.module.WasmInstruction.Type;
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
import de.inetsoftware.jwebassembly.wasm.StorageType;
import de.inetsoftware.jwebassembly.wasm.StructOperator;
import de.inetsoftware.jwebassembly.wasm.ValueType;
@ -56,6 +54,15 @@ class WasmStructInstruction extends WasmInstruction {
this.type = type;
}
/**
* Get the storage type of this instruction.
*
* @return the type
*/
StorageType getStorageType() {
return type;
}
/**
* {@inheritDoc}
*/