All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.python.compiler.Label Maven / Gradle / Ivy

// Copyright (c) Corporation for National Research Initiatives

package org.python.compiler;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

class Label {
    int position;
    int[] offsets, positions, sizes;
    int noffsets;
    Code code;
    int stack;

    public Label(Code code) {
        this.code = code;
        position = -1;
        noffsets = 0;
        offsets = new int[4];
        positions = new int[4];
        sizes = new int[4];
        stack = -1;
    }

    public void fix(byte[] data) throws IOException {
        ByteArrayOutputStream array = new ByteArrayOutputStream();
        DataOutputStream stream = new DataOutputStream(array);

        if (noffsets > 0 && position == -1)
            throw new InternalError("position never set for label");

        for (int i=0; i>> 8);
            //data[p+1] = (byte)(off & 0xff00);
        }
    }


    public void setStack(int stack) {
        if (this.stack == -1) {
            this.stack = stack;
        } else {
            if (this.stack != stack) {
                throw new InternalError("stack sizes don't agree: "+
                                        this.stack+", "+stack);
            }
        }
    }

    public int getPosition() {
        if (position == -1)
            throw new InternalError("position never set for label");
        return position;
    }

    public void setPosition() {
        position = code.size();
        //code.addLabel(this);
    }

    public void setBranch(int offset, int size) throws IOException {
        if (noffsets >= offsets.length) {
            int[] new_offsets = new int[offsets.length*2];
            System.arraycopy(offsets, 0, new_offsets, 0, noffsets);
            offsets = new_offsets;

            int[] new_positions = new int[positions.length*2];
            System.arraycopy(positions, 0, new_positions, 0, noffsets);
            positions = new_positions;

            int[] new_sizes = new int[sizes.length*2];
            System.arraycopy(sizes, 0, new_sizes, 0, noffsets);
            sizes = new_sizes;
        }
        positions[noffsets] = code.size();
        offsets[noffsets] = offset;
        sizes[noffsets] = size;
        noffsets = noffsets+1;
        if (size == 2) {
            code.code.writeShort(0);
        } else {
            code.code.writeInt(0);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy