| 
									
										
										
										
											2014-04-15 23:50:06 +05:30
										 |  |  | # This script generates the opcode.h header file. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import sys | 
					
						
							| 
									
										
										
										
											2016-11-28 18:13:52 +01:00
										 |  |  | import tokenize | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-19 01:23:18 +05:30
										 |  |  | header = """
 | 
					
						
							|  |  |  | /* Auto-generated by Tools/scripts/generate_opcode_h.py from Lib/opcode.py */ | 
					
						
							| 
									
										
										
										
											2014-04-15 23:50:06 +05:30
										 |  |  | #ifndef Py_OPCODE_H | 
					
						
							|  |  |  | #define Py_OPCODE_H | 
					
						
							|  |  |  | #ifdef __cplusplus | 
					
						
							|  |  |  | extern "C" { | 
					
						
							|  |  |  | #endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Instruction opcodes for compiled code */ | 
					
						
							| 
									
										
										
										
											2018-10-19 01:23:18 +05:30
										 |  |  | """.lstrip()
 | 
					
						
							| 
									
										
										
										
											2014-04-15 23:50:06 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | footer = """
 | 
					
						
							|  |  |  | /* EXCEPT_HANDLER is a special, implicit block type which is created when | 
					
						
							|  |  |  |    entering an except handler. It is not an opcode but we define it here | 
					
						
							|  |  |  |    as we want it to be available to both frameobject.c and ceval.c, while | 
					
						
							|  |  |  |    remaining private.*/ | 
					
						
							|  |  |  | #define EXCEPT_HANDLER 257 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define HAS_ARG(op) ((op) >= HAVE_ARGUMENT) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef __cplusplus | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif | 
					
						
							|  |  |  | #endif /* !Py_OPCODE_H */ | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-17 01:13:29 +02:00
										 |  |  | def main(opcode_py, outfile='Include/opcode.h'): | 
					
						
							|  |  |  |     opcode = {} | 
					
						
							| 
									
										
										
										
											2016-11-28 18:13:52 +01:00
										 |  |  |     if hasattr(tokenize, 'open'): | 
					
						
							|  |  |  |         fp = tokenize.open(opcode_py)   # Python 3.2+ | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         fp = open(opcode_py)            # Python 2.7 | 
					
						
							|  |  |  |     with fp: | 
					
						
							| 
									
										
										
										
											2016-11-25 11:59:52 +01:00
										 |  |  |         code = fp.read() | 
					
						
							|  |  |  |     exec(code, opcode) | 
					
						
							| 
									
										
										
										
											2014-04-17 01:13:29 +02:00
										 |  |  |     opmap = opcode['opmap'] | 
					
						
							| 
									
										
										
										
											2014-04-15 23:50:06 +05:30
										 |  |  |     with open(outfile, 'w') as fobj: | 
					
						
							|  |  |  |         fobj.write(header) | 
					
						
							| 
									
										
										
										
											2014-04-17 01:13:29 +02:00
										 |  |  |         for name in opcode['opname']: | 
					
						
							|  |  |  |             if name in opmap: | 
					
						
							| 
									
										
										
										
											2015-05-27 21:31:33 +03:00
										 |  |  |                 fobj.write("#define %-23s %3s\n" % (name, opmap[name])) | 
					
						
							| 
									
										
										
										
											2014-04-15 23:50:06 +05:30
										 |  |  |             if name == 'POP_EXCEPT': # Special entry for HAVE_ARGUMENT | 
					
						
							| 
									
										
										
										
											2015-05-27 21:31:33 +03:00
										 |  |  |                 fobj.write("#define %-23s %3d\n" % | 
					
						
							| 
									
										
										
										
											2014-04-17 01:13:29 +02:00
										 |  |  |                             ('HAVE_ARGUMENT', opcode['HAVE_ARGUMENT'])) | 
					
						
							| 
									
										
										
										
											2014-04-15 23:50:06 +05:30
										 |  |  |         fobj.write(footer) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-28 18:13:52 +01:00
										 |  |  |     print("%s regenerated from %s" % (outfile, opcode_py)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-15 23:50:06 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											2014-04-17 01:13:29 +02:00
										 |  |  |     main(sys.argv[1], sys.argv[2]) |