/* $Id: gen-event-codes.c,v 1.2 2003/02/17 00:30:01 mikpe Exp $
 *
 * Generate symbolic constants for performance counter events.
 *
 * Copyright (C) 2003  Mikael Pettersson
 */
#include <stdio.h>
#include <stdlib.h>	/* for exit() */
#include "libperfctr.h"

/* detect sharing in a DAG of immutable nodes */
static int test_and_set(const struct perfctr_event_set *event_set)
{
    static const struct perfctr_event_set *marked[32];
    unsigned int i;

    for(i = 0; i < sizeof(marked)/sizeof(marked[0]); ++i) {
	if( !marked[i] ) {
	    marked[i] = event_set;
	    return 0;
	}
	if( marked[i] == event_set )
	    return 1;
    }
    fprintf(stderr, "gen-event-set: too many distinct event sets\n");
    exit(1);
    /*NOTREACHED*/
}

static void print_event_set(const struct perfctr_event_set *event_set)
{
    unsigned int i;

    if( test_and_set(event_set) )
	return;
    if( event_set->include )
	print_event_set(event_set->include);
    printf("\n");
    for(i = 0; i < event_set->nevents; ++i)
	printf("#define %s%s\t0x%02X\n",
	       event_set->event_prefix,
	       event_set->events[i].name,
	       event_set->events[i].evntsel);
}

static void print_cpu_type(unsigned int cpu_type)
{
    print_event_set(perfctr_cpu_event_set(cpu_type));
}

int main(void)
{
    printf("/* automatically generated, do not edit */\n");
    print_cpu_type(PERFCTR_X86_INTEL_P5);
    print_cpu_type(PERFCTR_X86_INTEL_P5MMX);
    print_cpu_type(PERFCTR_X86_INTEL_P6);
    print_cpu_type(PERFCTR_X86_INTEL_PII);
    print_cpu_type(PERFCTR_X86_INTEL_PIII);
    print_cpu_type(PERFCTR_X86_CYRIX_MII);
    print_cpu_type(PERFCTR_X86_VIA_C3);
    print_cpu_type(PERFCTR_X86_WINCHIP_C6);
    print_cpu_type(PERFCTR_X86_WINCHIP_2);
    print_cpu_type(PERFCTR_X86_AMD_K7);
    print_cpu_type(PERFCTR_X86_AMD_K8);
    return 0;
}
