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