<commit_before>/*******************************************************************************
 *  _  _ ___ ___     _ _
 * | \| | __/ __| __| | |__
 * | .` | _|\__ \/ _` | '_ \
 * |_|\_|_| |___/\__,_|_.__/
 *
 * Copyright (c) 2014-2016. The NFSdb project and its contributors.
 *
 * 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 com.nfsdb.misc;

import com.nfsdb.io.sink.AbstractCharSink;
import com.nfsdb.io.sink.CharSink;
import com.nfsdb.std.Mutable;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class BytecodeAssembler implements Mutable {

    public static final int iconst_0 = 3;
    public static final int bipush = 16;
    public static final int sipush = 17;
    public static final int iload_2 = 28;
    public static final int aload_0 = 42;
    public static final int aload_1 = 43;
    public static final int istore_2 = 61;
    public static final int ifne = 154;
    public static final int ireturn = 172;
    public static final int return_ = 177;
    public static final int getfield = 180;
    public static final int putfield = 181;
    public static final int invokespecial = 183;
    public static final int invokestatic = 184;
    public static final int invokeinterface = 185;
    private static final int O_POOL_COUNT = 8;

    private final Utf8Appender utf8Appender = new Utf8Appender();
    private ByteBuffer buf;
    private int poolCount;
    private int objectClassIndex;
    private int defaultConstructorNameIndex;
    private int defaultConstructorDescIndex;
    private int defaultConstructorMethodIndex;
    private int codeAttributeIndex;
    private int methodCut1;
    private int methodCut2;

    public BytecodeAssembler() {
        this.buf = ByteBuffer.allocate(1024).order(ByteOrder.BIG_ENDIAN);
        this.poolCount = 1;
    }

    @Override
    public void clear() {
        this.buf.clear();
        this.poolCount = 1;
    }

    public void defineClass(int flags, int thisClassIndex) {
        // access flags
        putShort(flags);
        // this class index
        putShort(thisClassIndex);
        // super class
        putShort(objectClassIndex);
    }

    public void defineDefaultConstructor() {
        // constructor method entry
        startMethod(1, defaultConstructorNameIndex, defaultConstructorDescIndex, 1, 1);
        // code
        put(aload_0);
        put(invokespecial);
        putShort(defaultConstructorMethodIndex);
        put(return_);
        endMethodCode();
        // exceptions
        putShort(0);
        // attribute count
        putShort(0);
        endMethod();
    }

    public void defineField(int flags, int nameIndex, int typeIndex) {
        putShort(flags);
        putShort(nameIndex);
        putShort(typeIndex);
        // attribute count
        putShort(0);
    }

    public void dump(String path) {
        try (FileOutputStream fos = new FileOutputStream(path)) {
            int p = buf.position();
            int l = buf.limit();
            buf.flip();
            fos.getChannel().write(buf);
            buf.limit(l);
            buf.position(p);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void endMethod() {
        putInt(methodCut1, position() - methodCut1 - 4);
    }

    public void endMethodCode() {
        putInt(methodCut2, position() - methodCut2 - 4);
    }

    public void finishPool() {
        putShort(O_POOL_COUNT, poolCount);
    }

    public byte get(int pos) {
        return buf.get(pos);
    }

    public void invokeInterface(int interfaceIndex) {
        put(invokeinterface);
        putShort(interfaceIndex);
        put(0x02);
        put(0);
    }

    @SuppressWarnings("unchecked")
    public <T> Class<T> loadClass(Class host) {
        byte b[] = new byte[position()];
        System.arraycopy(buf.array(), 0, b, 0, b.length);
        return Unsafe.getUnsafe().defineAnonymousClass(host, b, null);
    }

    public int poolClass(int classIndex) {
        put(0x07);
        putShort(classIndex);
        return poolCount++;
    }

    public int poolField(int classIndex, int nameAndTypeIndex) {
        return poolRef(0x09, classIndex, nameAndTypeIndex);
    }

    public int poolInterfaceMethod(int classIndex, int nameAndTypeIndex) {
        return poolRef(0x0B, classIndex, nameAndTypeIndex);
    }

    public int poolMethod(int classIndex, int nameAndTypeIndex) {
        return poolRef(0x0A, classIndex, nameAndTypeIndex);
    }

    public int poolNameAndType(int nameIndex, int typeIndex) {
        return poolRef(0x0C, nameIndex, typeIndex);
    }

    public Utf8Appender poolUtf8() {
        put(0x01);<commit_msg>compilation erro fix (8-specific)<commit_after>/*******************************************************************************
 *  _  _ ___ ___     _ _
 * | \| | __/ __| __| | |__
 * | .` | _|\__ \/ _` | '_ \
 * |_|\_|_| |___/\__,_|_.__/
 *
 * Copyright (c) 2014-2016. The NFSdb project and its contributors.
 *
 * 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 com.nfsdb.misc;

import com.nfsdb.io.sink.AbstractCharSink;
import com.nfsdb.io.sink.CharSink;
import com.nfsdb.std.Mutable;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class BytecodeAssembler implements Mutable {

    public static final int iconst_0 = 3;
    public static final int bipush = 16;
    public static final int sipush = 17;
    public static final int iload_2 = 28;
    public static final int aload_0 = 42;
    public static final int aload_1 = 43;
    public static final int istore_2 = 61;
    public static final int ifne = 154;
    public static final int ireturn = 172;
    public static final int return_ = 177;
    public static final int getfield = 180;
    public static final int putfield = 181;
    public static final int invokespecial = 183;
    public static final int invokestatic = 184;
    public static final int invokeinterface = 185;
    private static final int O_POOL_COUNT = 8;

    private final Utf8Appender utf8Appender = new Utf8Appender();
    private ByteBuffer buf;
    private int poolCount;
    private int objectClassIndex;
    private int defaultConstructorNameIndex;
    private int defaultConstructorDescIndex;
    private int defaultConstructorMethodIndex;
    private int codeAttributeIndex;
    private int methodCut1;
    private int methodCut2;

    public BytecodeAssembler() {
        this.buf = ByteBuffer.allocate(1024).order(ByteOrder.BIG_ENDIAN);
        this.poolCount = 1;
    }

    @Override
    public void clear() {
        this.buf.clear();
        this.poolCount = 1;
    }

    public void defineClass(int flags, int thisClassIndex) {
        // access flags
        putShort(flags);
        // this class index
        putShort(thisClassIndex);
        // super class
        putShort(objectClassIndex);
    }

    public void defineDefaultConstructor() {
        // constructor method entry
        startMethod(1, defaultConstructorNameIndex, defaultConstructorDescIndex, 1, 1);
        // code
        put(aload_0);
        put(invokespecial);
        putShort(defaultConstructorMethodIndex);
        put(return_);
        endMethodCode();
        // exceptions
        putShort(0);
        // attribute count
        putShort(0);
        endMethod();
    }

    public void defineField(int flags, int nameIndex, int typeIndex) {
        putShort(flags);
        putShort(nameIndex);
        putShort(typeIndex);
        // attribute count
        putShort(0);
    }

    public void dump(String path) {
        try (FileOutputStream fos = new FileOutputStream(path)) {
            int p = buf.position();
            int l = buf.limit();
            buf.flip();
            fos.getChannel().write(buf);
            buf.limit(l);
            buf.position(p);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void endMethod() {
        putInt(methodCut1, position() - methodCut1 - 4);
    }

    public void endMethodCode() {
        putInt(methodCut2, position() - methodCut2 - 4);
    }

    public void finishPool() {
        putShort(O_POOL_COUNT, poolCount);
    }

    public byte get(int pos) {
        return buf.get(pos);
    }

    public void invokeInterface(int interfaceIndex) {
        put(invokeinterface);
        putShort(interfaceIndex);
        put(0x02);
        put(0);
    }

    @SuppressWarnings("unchecked")
    public <T> Class<T> loadClass(Class<?> host) {
        byte b[] = new byte[position()];
        System.arraycopy(buf.array(), 0, b, 0, b.length);
        return (Class<T>) Unsafe.getUnsafe().defineAnonymousClass(host, b, null);
    }

    public int poolClass(int classIndex) {
        put(0x07);
        putShort(classIndex);
        return poolCount++;
    }

    public int poolField(int classIndex, int nameAndTypeIndex) {
        return poolRef(0x09, classIndex, nameAndTypeIndex);
    }

    public int poolInterfaceMethod(int classIndex, int nameAndTypeIndex) {
        return poolRef(0x0B, classIndex, nameAndTypeIndex);
    }

    public int poolMethod(int classIndex, int nameAndTypeIndex) {
        return poolRef(0x0A, classIndex, nameAndTypeIndex);
    }

    public int poolNameAndType(int nameIndex, int typeIndex) {
        return poolRef(0x0C, nameIndex, typeIndex);
    }

    public Utf8Appender poolUtf8() {
        put(0x01);
{'fraction_non_alphanumeric': 0.12032336905433352, 'fraction_numerical': 0.019270539575108103, 'mean_word_length': 2.71993006993007, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6885647', 'n_tokens_mistral': 3150, 'n_tokens_neox': 2969, 'n_words': 865}