1# This script generates the opcode.h header file. 2 3import sys 4import tokenize 5 6header = """ 7/* Auto-generated by Tools/scripts/generate_opcode_h.py from Lib/opcode.py */ 8#ifndef Py_OPCODE_H 9#define Py_OPCODE_H 10#ifdef __cplusplus 11extern "C" { 12#endif 13 14 15 /* Instruction opcodes for compiled code */ 16""".lstrip() 17 18footer = """ 19/* EXCEPT_HANDLER is a special, implicit block type which is created when 20 entering an except handler. It is not an opcode but we define it here 21 as we want it to be available to both frameobject.c and ceval.c, while 22 remaining private.*/ 23#define EXCEPT_HANDLER 257 24 25#define HAS_ARG(op) ((op) >= HAVE_ARGUMENT) 26 27#ifdef __cplusplus 28} 29#endif 30#endif /* !Py_OPCODE_H */ 31""" 32 33 34def main(opcode_py, outfile='Include/opcode.h'): 35 opcode = {} 36 if hasattr(tokenize, 'open'): 37 fp = tokenize.open(opcode_py) # Python 3.2+ 38 else: 39 fp = open(opcode_py) # Python 2.7 40 with fp: 41 code = fp.read() 42 exec(code, opcode) 43 opmap = opcode['opmap'] 44 with open(outfile, 'w') as fobj: 45 fobj.write(header) 46 for name in opcode['opname']: 47 if name in opmap: 48 fobj.write("#define %-23s %3s\n" % (name, opmap[name])) 49 if name == 'POP_EXCEPT': # Special entry for HAVE_ARGUMENT 50 fobj.write("#define %-23s %3d\n" % 51 ('HAVE_ARGUMENT', opcode['HAVE_ARGUMENT'])) 52 fobj.write(footer) 53 54 print("%s regenerated from %s" % (outfile, opcode_py)) 55 56 57if __name__ == '__main__': 58 main(sys.argv[1], sys.argv[2]) 59