HomeForumSourceResearchGuide
Sign in to contribute to source. how it works
Component util.compiler.LiteralsTable by barry
expand copy to clipboardexpand
data Literal {
	byte value[]
}

component provides LiteralsTable requires data.adt.List, io.Output out, data.IntUtil iu {
	
	List literals
	int lengthBytes
	int tableOffset

	LiteralsTable:LiteralsTable()
		{
		literals = new List()
		lengthBytes = 0
		}
	
	void copyBytes(byte dest[], int destOffset, byte src[])
		{
		for (int i = 0; i < src.arrayLength; i++)
			{
			dest[destOffset+i] = src[i]
			}
		}
	
	void LiteralsTable:add(store byte value[])
		{
		literals.add(new Literal(value))
		lengthBytes += value.arrayLength
		}
	
	byte[] LiteralsTable:getBytes()
		{
		//TODO: we could do lossless string-table compression here

		byte result[] = new byte[lengthBytes]
		int offset = 0
		for (Literal l = literals.getFirst(); l != null; l = literals.getNext())
			{
			copyBytes(result, offset, l.value)
			offset += l.value.arrayLength
			}
		
		return result
		}

	void LiteralsTable:setOffset(int offset)
		{
		tableOffset = offset
		}
	
	int LiteralsTable:getOffsetOf(byte value[])
		{
		int offset = 0
		for (Literal l = literals.getFirst(); l != null; l = literals.getNext())
			{
			if (l.value.arrayLength == value.arrayLength && l.value == value)
				{
				int result = tableOffset + offset
				return result
				}
			offset += l.value.arrayLength
			}
		
		throw new Exception("literal of $(value.arrayLength)-bytes not found in literal table")
		}
	
	LiteralEntry[] LiteralsTable:getOffsets()
		{
		LiteralEntry result[] = new LiteralEntry[literals.getLength()]

		int offset = tableOffset
		int i = 0
		for (Literal l = literals.getFirst(); l != null; l = literals.getNext())
			{
			result[i] = new LiteralEntry(l.value, offset)
			offset += l.value.arrayLength
			i ++
			}
		
		return result
		}
	
	}
Revision history
To propose a new revision to this entity, use dana source put -uc your/new/version.dn -n util.compiler.LiteralsTable -m "reason for update" -u yourUsername
Version 1 (this version) by barry
Notes for this version: New compiler components