generate_opcode_h.py 1.7 KB

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