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 33UINT32_MASK = (1<<32)-1 34 35def write_int_array_from_ops(name, ops, out): 36 bits = 0 37 for op in ops: 38 bits |= 1<<op 39 out.write(f"static uint32_t {name}[8] = {{\n") 40 for i in range(8): 41 out.write(f" {bits & UINT32_MASK}U,\n") 42 bits >>= 32 43 assert bits == 0 44 out.write(f"}};\n") 45 46def main(opcode_py, outfile='Include/opcode.h'): 47 opcode = {} 48 if hasattr(tokenize, 'open'): 49 fp = tokenize.open(opcode_py) # Python 3.2+ 50 else: 51 fp = open(opcode_py) # Python 2.7 52 with fp: 53 code = fp.read() 54 exec(code, opcode) 55 opmap = opcode['opmap'] 56 hasjrel = opcode['hasjrel'] 57 hasjabs = opcode['hasjabs'] 58 with open(outfile, 'w') as fobj: 59 fobj.write(header) 60 for name in opcode['opname']: 61 if name in opmap: 62 fobj.write("#define %-23s %3s\n" % (name, opmap[name])) 63 if name == 'POP_EXCEPT': # Special entry for HAVE_ARGUMENT 64 fobj.write("#define %-23s %3d\n" % 65 ('HAVE_ARGUMENT', opcode['HAVE_ARGUMENT'])) 66 fobj.write("#ifdef NEED_OPCODE_JUMP_TABLES\n") 67 write_int_array_from_ops("_PyOpcode_RelativeJump", opcode['hasjrel'], fobj) 68 write_int_array_from_ops("_PyOpcode_Jump", opcode['hasjrel'] + opcode['hasjabs'], fobj) 69 fobj.write("#endif /* OPCODE_TABLES */\n") 70 fobj.write(footer) 71 72 73 print("%s regenerated from %s" % (outfile, opcode_py)) 74 75 76if __name__ == '__main__': 77 main(sys.argv[1], sys.argv[2]) 78