<commit_before>/*
 *  Copyright 2017 <NAME>
 *
 *  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.
 */

#include <math.h>
#include <stddef.h>

#include "warp-buf.h"
#include "warp-error.h"
#include "warp-execution.h"
#include "warp-macros.h"
#include "warp-stack-ops.h"
#include "warp-wasm.h"
#include "warp.h"

static wrp_err_t exec_invalid_op(wrp_vm_t *vm)
{
    return WRP_ERR_INVALID_OPCODE;
}

static wrp_err_t exec_unreachable_op(wrp_vm_t *vm)
{
    return WRP_ERR_UNREACHABLE_CODE_EXECUTED;
}

static wrp_err_t exec_no_op(wrp_vm_t *vm)
{
    return WRP_SUCCESS;
}

static wrp_err_t exec_block_op(wrp_vm_t *vm)
{
    size_t block_address = vm->opcode_stream.pos - 1;
    uint32_t func_idx = vm->call_stk[vm->call_stk_head].func_idx;
    int8_t signature = 0;
    WRP_CHECK(wrp_read_vari7(&vm->opcode_stream, &signature));

    uint32_t block_idx = 0;
    WRP_CHECK(wrp_get_block_idx(vm->mdle, func_idx, block_address, &block_idx));
    WRP_CHECK(wrp_stk_exec_push_block(vm, vm->mdle->block_labels[block_idx], BLOCK, signature))

    return WRP_SUCCESS;
}

static wrp_err_t exec_loop_op(wrp_vm_t *vm)
{
    return WRP_ERR_UNKNOWN;
}

static wrp_err_t exec_if_op(wrp_vm_t *vm)
{
    size_t if_address = vm->opcode_stream.pos - 1;
    uint32_t func_idx = vm->call_stk[vm->call_stk_head].func_idx;
    int8_t signature = 0;
    WRP_CHECK(wrp_read_vari7(&vm->opcode_stream, &signature));

    if (!wrp_is_valid_block_signature(signature)) {
        return WRP_ERR_INVALID_BLOCK_SIGNATURE;
    }

    uint32_t if_idx = 0;
    WRP_CHECK(wrp_get_if_idx(vm->mdle, func_idx, if_address, &if_idx));

    int32_t condition = 0;
    WRP_CHECK(wrp_stk_exec_pop_i32(vm, &condition));

    if (condition != 0 || vm->mdle->else_addresses[if_idx] != 0) {
        WRP_CHECK(wrp_stk_exec_push_block(vm, vm->mdle->if_labels[if_idx], BLOCK_IF, signature));
    }

    if (condition == 0 && vm->mdle->else_addresses[if_idx] == 0) {
        vm->opcode_stream.pos = vm->mdle->if_labels[if_idx] + 1;
    }

    if (condition == 0 && vm->mdle->else_addresses[if_idx] != 0) {
        vm->opcode_stream.pos = vm->mdle->else_addresses[if_idx] + 1;
    }

    return WRP_SUCCESS;
}

static wrp_err_t exec_else_op(wrp_vm_t *vm)
{
    WRP_CHECK(wrp_stk_exec_pop_block(vm, 0));
    return WRP_SUCCESS;
}

static wrp_err_t exec_end_op(wrp_vm_t *vm)
{
    WRP_CHECK(wrp_stk_exec_pop_block(vm, 0));
    return WRP_SUCCESS;
}

static wrp_err_t exec_br_op(wrp_vm_t *vm)
{
    uint32_t depth = 0;
    WRP_CHECK(wrp_read_varui32(&vm->opcode_stream, &depth));
    WRP_CHECK(wrp_stk_exec_pop_block(vm, depth))
    return WRP_SUCCESS;
}

static wrp_err_t exec_br_if_op(wrp_vm_t *vm)
{
    uint32_t depth = 0;
    WRP_CHECK(wrp_read_varui32(&vm->opcode_stream, &depth));

    int32_t condition = 0;
    WRP_CHECK(wrp_stk_exec_pop_i32(vm, &condition));

    if (condition != 0) {
        WRP_CHECK(wrp_stk_exec_pop_block(vm, depth));
    }

    return WRP_SUCCESS;
}

static wrp_err_t exec_br_table_op(wrp_vm_t *vm)
{
    uint32_t target_count = 0;
    WRP_CHECK(wrp_read_varui32(&vm->opcode_stream, &target_count));

    //TODO handle any table size
    if (target_count > MAX_BRANCH_TABLE_SIZE) {
        return WRP_ERR_MDLE_BRANCH_TABLE_OVERFLOW;
    }

    uint32_t branch_table[MAX_BRANCH_TABLE_SIZE] = {0};

    for (uint32_t i = 0; i < target_count; i++) {
        WRP_CHECK(wrp_read_varui32(&vm->opcode_stream, &branch_table[i]));
    }

    uint32_t default_target = 0;
    WRP_CHECK(wrp_read_varui32(&vm->opcode_stream, &default_target));

    int32_t target_idx = 0;
    WRP_CHECK(wrp_stk_exec_pop_i32(vm, &target_idx));

    int32_t depth = default_target;

    if (target_idx >= 0 && (uint32_t)target_idx < target_count) {
        depth = target_idx;
    }

    WRP_CHECK(wrp_stk_exec_pop_block(vm, depth));
    return WRP_SUCCESS;
}

static wrp_err_t exec_return_op(wrp_vm_t *vm)
{
    return WRP_ERR_UNKNOWN;
}

static wrp_err_t exec_call_op(wrp_vm_t *vm)
{
    uint32_t target_idx = 0;
    WRP_CHECK(wrp_read_varui32(&vm->opcode_stream, &target_idx));
    WRP_CHECK(wrp_stk_exec_push_call(vm, target_idx));
    return WRP_SUCCESS;
}

static wrp_err_t exec_call_indirect_op(wrp_vm_t *vm)
{
    return WRP_ERR_UNKNOWN;
}

static wrp_err_t exec_drop_op(wrp_vm_t *vm)
{
    return WRP_ERR_UNKNOWN;
}

static wrp_err_t exec_select_op(wrp_vm_t *vm)
{<commit_msg>fixed branch table bug<commit_after>/*
 *  Copyright 2017 <NAME>
 *
 *  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.
 */

#include <math.h>
#include <stddef.h>

#include "warp-buf.h"
#include "warp-error.h"
#include "warp-execution.h"
#include "warp-macros.h"
#include "warp-stack-ops.h"
#include "warp-wasm.h"
#include "warp.h"

static wrp_err_t exec_invalid_op(wrp_vm_t *vm)
{
    return WRP_ERR_INVALID_OPCODE;
}

static wrp_err_t exec_unreachable_op(wrp_vm_t *vm)
{
    return WRP_ERR_UNREACHABLE_CODE_EXECUTED;
}

static wrp_err_t exec_no_op(wrp_vm_t *vm)
{
    return WRP_SUCCESS;
}

static wrp_err_t exec_block_op(wrp_vm_t *vm)
{
    size_t block_address = vm->opcode_stream.pos - 1;
    uint32_t func_idx = vm->call_stk[vm->call_stk_head].func_idx;
    int8_t signature = 0;
    WRP_CHECK(wrp_read_vari7(&vm->opcode_stream, &signature));

    uint32_t block_idx = 0;
    WRP_CHECK(wrp_get_block_idx(vm->mdle, func_idx, block_address, &block_idx));
    WRP_CHECK(wrp_stk_exec_push_block(vm, vm->mdle->block_labels[block_idx], BLOCK, signature))

    return WRP_SUCCESS;
}

static wrp_err_t exec_loop_op(wrp_vm_t *vm)
{
    return WRP_ERR_UNKNOWN;
}

static wrp_err_t exec_if_op(wrp_vm_t *vm)
{
    size_t if_address = vm->opcode_stream.pos - 1;
    uint32_t func_idx = vm->call_stk[vm->call_stk_head].func_idx;
    int8_t signature = 0;
    WRP_CHECK(wrp_read_vari7(&vm->opcode_stream, &signature));

    if (!wrp_is_valid_block_signature(signature)) {
        return WRP_ERR_INVALID_BLOCK_SIGNATURE;
    }

    uint32_t if_idx = 0;
    WRP_CHECK(wrp_get_if_idx(vm->mdle, func_idx, if_address, &if_idx));

    int32_t condition = 0;
    WRP_CHECK(wrp_stk_exec_pop_i32(vm, &condition));

    if (condition != 0 || vm->mdle->else_addresses[if_idx] != 0) {
        WRP_CHECK(wrp_stk_exec_push_block(vm, vm->mdle->if_labels[if_idx], BLOCK_IF, signature));
    }

    if (condition == 0 && vm->mdle->else_addresses[if_idx] == 0) {
        vm->opcode_stream.pos = vm->mdle->if_labels[if_idx] + 1;
    }

    if (condition == 0 && vm->mdle->else_addresses[if_idx] != 0) {
        vm->opcode_stream.pos = vm->mdle->else_addresses[if_idx] + 1;
    }

    return WRP_SUCCESS;
}

static wrp_err_t exec_else_op(wrp_vm_t *vm)
{
    WRP_CHECK(wrp_stk_exec_pop_block(vm, 0));
    return WRP_SUCCESS;
}

static wrp_err_t exec_end_op(wrp_vm_t *vm)
{
    WRP_CHECK(wrp_stk_exec_pop_block(vm, 0));
    return WRP_SUCCESS;
}

static wrp_err_t exec_br_op(wrp_vm_t *vm)
{
    uint32_t depth = 0;
    WRP_CHECK(wrp_read_varui32(&vm->opcode_stream, &depth));
    WRP_CHECK(wrp_stk_exec_pop_block(vm, depth))
    return WRP_SUCCESS;
}

static wrp_err_t exec_br_if_op(wrp_vm_t *vm)
{
    uint32_t depth = 0;
    WRP_CHECK(wrp_read_varui32(&vm->opcode_stream, &depth));

    int32_t condition = 0;
    WRP_CHECK(wrp_stk_exec_pop_i32(vm, &condition));

    if (condition != 0) {
        WRP_CHECK(wrp_stk_exec_pop_block(vm, depth));
    }

    return WRP_SUCCESS;
}

static wrp_err_t exec_br_table_op(wrp_vm_t *vm)
{
    uint32_t target_count = 0;
    WRP_CHECK(wrp_read_varui32(&vm->opcode_stream, &target_count));

    //TODO handle any table size
    if (target_count > MAX_BRANCH_TABLE_SIZE) {
        return WRP_ERR_MDLE_BRANCH_TABLE_OVERFLOW;
    }

    uint32_t branch_table[MAX_BRANCH_TABLE_SIZE] = {0};

    for (uint32_t i = 0; i < target_count; i++) {
        WRP_CHECK(wrp_read_varui32(&vm->opcode_stream, &branch_table[i]));
    }

    uint32_t default_target = 0;
    WRP_CHECK(wrp_read_varui32(&vm->opcode_stream, &default_target));

    int32_t target_idx = 0;
    WRP_CHECK(wrp_stk_exec_pop_i32(vm, &target_idx));

    int32_t depth = default_target;

    if (target_idx >= 0 && (uint32_t)target_idx < target_count) {
        depth = branch_table[target_idx];
    }

    WRP_CHECK(wrp_stk_exec_pop_block(vm, depth));
    return WRP_SUCCESS;
}

static wrp_err_t exec_return_op(wrp_vm_t *vm)
{
    return WRP_ERR_UNKNOWN;
}

static wrp_err_t exec_call_op(wrp_vm_t *vm)
{
    uint32_t target_idx = 0;
    WRP_CHECK(wrp_read_varui32(&vm->opcode_stream, &target_idx));
    WRP_CHECK(wrp_stk_exec_push_call(vm, target_idx));
    return WRP_SUCCESS;
}

static wrp_err_t exec_call_indirect_op(wrp_vm_t *vm)
{
    return WRP_ERR_UNKNOWN;
}

static wrp_err_t exec_drop_op(wrp_vm_t *vm)
{
    return WRP_ERR_UNKNOWN;
}

static wrp_err_t exec_select_op(wrp_vm_t *vm)
{
{'fraction_non_alphanumeric': 0.11329694098259346, 'fraction_numerical': 0.018539499433515294, 'mean_word_length': 4.231681034482759, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 77, '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': '1638837', 'n_tokens_mistral': 4154, 'n_tokens_neox': 4031, 'n_words': 707}