/**
* Copyright 2012 Tobias Gierke <tobias.gierke@code-sourcery.de>
*
* 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.codesourcery.jasm16.ast;
import java.io.IOException;
import de.codesourcery.jasm16.Address;
import de.codesourcery.jasm16.ISymbolAware;
import de.codesourcery.jasm16.compiler.ICompilationContext;
import de.codesourcery.jasm16.compiler.io.IObjectCodeWriter;
import de.codesourcery.jasm16.exceptions.ParseException;
/**
* Abstract base-class for AST nodes that generate object code.
*
* @author tobias.gierke@code-sourcery.de
*/
public abstract class ObjectCodeOutputNode extends ASTNode implements ISymbolAware
{
public static final int UNKNOWN_SIZE = -1;
private Address address;
public ObjectCodeOutputNode() {
}
protected final void setAddress(Address address) {
this.address = address;
}
/**
* Returns the memory address where this node's content will
* start.
*
* @return address or <code>null</code> if the corresponding memory address
* is not yet known
*/
public final Address getAddress() {
return address;
}
/**
* Adjusts this node's address by a given word offset.
*
* @param wordOffset
* @throws IllegalStateException if this node has no address assigned
*/
public final void adjustAddress(int wordOffset) throws IllegalStateException {
if ( address == null ) {
throw new IllegalStateException("Address not set");
}
this.address = Address.wordAddress( address.getWordAddressValue()+wordOffset );
}
/**
* Returns the size of the object code generated by this node
* in bytes.
*
* @param thisNodesObjectCodeOffsetInBytes TODO
*
* @return size in bytes or {@link #UNKNOWN_SIZE} if this node doesn't know
* how to calculate it's size (either because {@link #symbolsResolved(ICompilationContext)} has not been
* called yet or this AST node didn't parse correctly).
*
* @see #symbolsResolved(ICompilationContext)
*/
public abstract int getSizeInBytes(long thisNodesObjectCodeOffsetInBytes);
/**
* Write this node's object code.
*
* @param writer
* @param compContext TODO
* @throws IOException
* @throws ParseException
*/
public abstract void writeObjectCode(IObjectCodeWriter writer, ICompilationContext compContext) throws IOException,ParseException;
}