JWebAssembly/src/de/inetsoftware/jwebassembly/module/WasmGlobalInstruction.java
2022-09-18 13:00:00 +02:00

127 lines
3.0 KiB
Java

/*
Copyright 2018 - 2022 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.io.IOException;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import de.inetsoftware.jwebassembly.wasm.AnyType;
/**
* WasmInstruction for set and get global variables.
*
* @author Volker Berlin
*
*/
class WasmGlobalInstruction extends WasmInstruction {
private boolean load;
private FunctionName name;
private AnyType type;
private FunctionName clinit;
/**
* Create an instance of a load/store instruction
*
* @param load
* true: if load or GET
* @param name
* the name of the static field
* @param type
* the type of the static field
* @param clinit
* a reference to the class/static constructor which should executed before access a static field
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
WasmGlobalInstruction( boolean load, @Nonnull FunctionName name, AnyType type, @Nullable FunctionName clinit, int javaCodePos, int lineNumber ) {
super( javaCodePos, lineNumber );
this.load = load;
this.name = name;
this.type = type;
this.clinit = clinit;
}
/**
* {@inheritDoc}
*/
@Override
Type getType() {
return Type.Global;
}
/**
* The name of the field
*
* @return the field
*/
@Nonnull
FunctionName getFieldName() {
return name;
}
/**
* {@inheritDoc}
*/
@Override
public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
if( clinit != null ) {
writer.writeFunctionCall( clinit, clinit.signatureName );
}
writer.writeGlobalAccess( load, name, type );
}
/**
* {@inheritDoc}
*/
@Override
AnyType getPushValueType() {
return load ? type : null;
}
/**
* {@inheritDoc}
*/
@Override
int getPopCount() {
return load ? 0 : 1;
}
/**
* {@inheritDoc}
*/
@Override
AnyType[] getPopValueTypes() {
return load ? null : new AnyType[] { type };
}
/**
* Only used for debugging
*/
@Override
public String toString() {
return getClass().getSimpleName() + ": global." + (load ? "get $" : "set $") + name.fullName;
}
}