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