package Filters;

use strict;
use warnings;
use diagnostics;

use FindBin;
use lib $FindBin::Bin;
use Util;

my %CONFIGS = (
    no_aot_instr => {
        label   => "Exclude WAMR AOT array (instr)",
        regions => [
            [
                '_wamr_aot_start', '_wamr_aot_end',
                'p.injection_instr_absolute'
            ]
        ],
    },
    no_aot_data => {
        label   => "Exclude WAMR AOT array (data)",
        regions =>
          [ [ '_wamr_aot_start', '_wamr_aot_end', 'g.data_physical_address' ] ],
    },
    no_mmap_instr => {
        label   => "Exclude WAMR mmap (instr)",
        regions => [
            [
                '_wamr_mmap_start', '_wamr_mmap_end',
                'p.injection_instr_absolute'
            ]
        ],
    },
    no_mmap_data => {
        label   => "Exclude WAMR mmap (data)",
        regions => [
            [ '_wamr_mmap_start', '_wamr_mmap_end', 'g.data_physical_address' ]
        ],
    },
    no_runtime_pool_instr => {
        label   => "Exclude WAMR runtime pool (instr)",
        regions => [
            [
                '_wamr_runtime_pool_start', '_wamr_runtime_pool_end',
                'p.injection_instr_absolute'
            ]
        ],
    },
    no_runtime_pool_data => {
        label   => "Exclude WAMR runtime pool (data)",
        regions => [
            [
                '_wamr_runtime_pool_start', '_wamr_runtime_pool_end',
                'g.data_physical_address'
            ]
        ],
    },
    no_linear_pool_instr => {
        label   => "Exclude WAMR linear pool (instr)",
        regions => [
            [
                '_wamr_linear_pool_start', '_wamr_linear_pool_end',
                'p.injection_instr_absolute'
            ]
        ],
    },
    no_linear_pool_data => {
        label   => "Exclude WAMR linear pool (data)",
        regions => [
            [
                '_wamr_linear_pool_start', '_wamr_linear_pool_end',
                'g.data_physical_address'
            ]
        ],
    },
    no_global_heap_instr => {
        label   => "Exclude WAMR global heap (instr)",
        regions => [
            [
                '_wamr_global_heap_start', '_wamr_global_heap_end',
                'p.injection_instr_absolute'
            ]
        ],
    },
    no_global_heap_data => {
        label   => "Exclude WAMR global heap (data)",
        regions => [
            [
                '_wamr_global_heap_start', '_wamr_global_heap_end',
                'g.data_physical_address'
            ]
        ],
    },
);

# Those will be executed automatically by runner.pl (+ no filter at all)
my @DEFAULT_CONFIGS = ('no_aot_data');

sub get_configs {
    return \%CONFIGS;
}

sub get_default_configs {
    return @DEFAULT_CONFIGS;
}

sub build_filter_clause {
    my ( $experiment_dir, $config_name ) = @_;

    return "" unless defined $config_name && length($config_name) > 0;

    my $regions = $CONFIGS{$config_name}{regions};
    return "" unless defined $regions;

    return ""
      unless defined $experiment_dir && -f "$experiment_dir/system.elf";

    my @filters;
    for my $pair (@$regions) {
        my ( $start_sym, $end_sym, $col ) = @$pair;
        my $start =
          Util::elf_sym_addr( "$experiment_dir/system.elf", $start_sym );
        my $end = Util::elf_sym_addr( "$experiment_dir/system.elf", $end_sym );
        next unless defined $start && defined $end && $end > $start;
        push @filters, "$col NOT BETWEEN $start AND @{[$end - 1]}";
    }

    return "" unless @filters;
    return "\nAND " . join( "\nAND ", @filters );
}

1;
