#!/usr/bin/env python3
# Generate trap codes list.
#
# (c) Mit authors 2018-2020
#
# The package is distributed under the MIT/X11 License.
#
# THIS PROGRAM IS PROVIDED AS IS, WITH NO WARRANTY. USE IS AT THE USER’S
# RISK.

from code_util import Code, copyright_banner, enum_to_c
import stack
stack.TYPE_SIZE_UNKNOWN = 0

from traps import *
from trap_errors import TrapErrorCode


GENERATOR_PROGRAM = 'gen-trap-codes'
PURPOSE = 'Trap codes.'
COPYRIGHT_YEARS = '2018-2020'

code = copyright_banner(GENERATOR_PROGRAM, PURPOSE, COPYRIGHT_YEARS)
code.append('''
#ifndef MIT_TRAP_CODES_H
#define MIT_TRAP_CODES_H


#define INSTRUCTION(name, opcode) name = opcode,
''')
code.append('')
code.extend(enum_to_c(LibInstructions, lambda i: i.opcode))
code.append('')
for lib_instruction in LibInstructions:
    code.extend(enum_to_c(lib_instruction.library, lambda i: i.opcode))
    code.append('')
code.append('#undef INSTRUCTION')
code.append('')
code.extend(enum_to_c(TrapErrorCode))
code.append('')
code.append('#endif')

print(code)
