#!/usr/bin/env python3

import subprocess
import os
import re
import datetime

def cpp_preprocess(input_path, cpp='cpp'):
    return subprocess.check_output([cpp, '-P', input_path]).decode()

def gen_isr_header(f, cpp='cpp'):
    stripped_code = cpp_preprocess(args.input, args.use_cpp)

    armed = False
    for line in stripped_code.splitlines():
        line = line.strip()

        if armed:
            if not line.startswith('.word'):
                break

            word, value = line.split()
            assert word == '.word'
            if value == '0':
                yield None
            else:
                yield value

        else:
            if line.startswith('g_pfnVectors:'):
                armed = True

    else:
        raise ValueError('Cannot find interrupt vector definition!')

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('--use-cpp', type=str, default=os.getenv('CPP', 'cpp'), help='cpp (C preprocessor) executable to use')
    parser.add_argument('-g', '--generate-include-guards', action='store_true', help='Whether to generate include guards')
    parser.add_argument('input', help='Input stm32****_startup.s file')
    args = parser.parse_args()

    print('/* AUTOGENERATED FILE! DO NOT MODIFY! */')
    print('/* Generated {datetime.datetime.utcnow()} from {args.input} */')
    if args.generate_include_guards:
        include_guard_id = '__ISR_HEADER_' + re.sub('[^A-Za-z0-9]', '_', args.input.split('/')[-1]) + '__'
        print(f'#ifndef {include_guard_id}')
        print(f'#define {include_guard_id}')

    print()
    for i, handler_name in enumerate(gen_isr_header(args.input, args.use_cpp)):
        if handler_name is None:
            print(f'/* IRQ {i} is undefined for this part. */')
        else:
            print(f'void {handler_name}(void); {" " * (30-len(handler_name))} /* {i:> 3} */')
    print()

    print(f'#define NUM_IRQs {i+1}')
    print('extern uint32_t g_pfnVectors[NUM_IRQs];')
    print('#define isr_vector g_pfnVectors')
    print()

    if args.generate_include_guards:
        print(f'#endif /* {include_guard_id} */')
    
