1*795d594fSAndroid Build Coastguard Worker#!/usr/bin/env python3 2*795d594fSAndroid Build Coastguard Worker# 3*795d594fSAndroid Build Coastguard Worker# Copyright (C) 2016 The Android Open Source Project 4*795d594fSAndroid Build Coastguard Worker# 5*795d594fSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*795d594fSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*795d594fSAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*795d594fSAndroid Build Coastguard Worker# 9*795d594fSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*795d594fSAndroid Build Coastguard Worker# 11*795d594fSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*795d594fSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*795d594fSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*795d594fSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*795d594fSAndroid Build Coastguard Worker# limitations under the License. 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Workerimport sys, re, os 18*795d594fSAndroid Build Coastguard Workerfrom io import StringIO 19*795d594fSAndroid Build Coastguard Worker 20*795d594fSAndroid Build Coastguard WorkerSCRIPT_DIR = os.path.dirname(sys.argv[0]) 21*795d594fSAndroid Build Coastguard Worker# This file is included verbatim at the start of the in-memory python script. 22*795d594fSAndroid Build Coastguard WorkerSCRIPT_SETUP_CODE = SCRIPT_DIR + "/common/gen_setup.py" 23*795d594fSAndroid Build Coastguard WorkerINTERP_DEFS_FILE = SCRIPT_DIR + "/../../../libdexfile/dex/dex_instruction_list.h" 24*795d594fSAndroid Build Coastguard WorkerNUM_PACKED_OPCODES = 256 25*795d594fSAndroid Build Coastguard Worker 26*795d594fSAndroid Build Coastguard Worker# Extract an ordered list of instructions from the VM sources. We use the 27*795d594fSAndroid Build Coastguard Worker# "goto table" definition macro, which has exactly NUM_PACKED_OPCODES entries. 28*795d594fSAndroid Build Coastguard Workerdef getOpcodeList(): 29*795d594fSAndroid Build Coastguard Worker opcodes = [] 30*795d594fSAndroid Build Coastguard Worker opcode_fp = open(INTERP_DEFS_FILE) 31*795d594fSAndroid Build Coastguard Worker opcode_re = re.compile(r"^\s*V\((....), (\w+),.*", re.DOTALL) 32*795d594fSAndroid Build Coastguard Worker for line in opcode_fp: 33*795d594fSAndroid Build Coastguard Worker match = opcode_re.match(line) 34*795d594fSAndroid Build Coastguard Worker if not match: 35*795d594fSAndroid Build Coastguard Worker continue 36*795d594fSAndroid Build Coastguard Worker opcodes.append("op_" + match.group(2).lower()) 37*795d594fSAndroid Build Coastguard Worker opcode_fp.close() 38*795d594fSAndroid Build Coastguard Worker 39*795d594fSAndroid Build Coastguard Worker if len(opcodes) != NUM_PACKED_OPCODES: 40*795d594fSAndroid Build Coastguard Worker print("ERROR: found ", len(opcodes), " opcodes in Interp.h (expected ", NUM_PACKED_OPCODES, ")") 41*795d594fSAndroid Build Coastguard Worker raise SyntaxError("bad opcode count") 42*795d594fSAndroid Build Coastguard Worker return opcodes 43*795d594fSAndroid Build Coastguard Worker 44*795d594fSAndroid Build Coastguard Workerindent_re = re.compile(r"^%( *)") 45*795d594fSAndroid Build Coastguard Worker 46*795d594fSAndroid Build Coastguard Worker# Finds variable references in text: $foo or ${foo} 47*795d594fSAndroid Build Coastguard Workerescape_re = re.compile(r''' 48*795d594fSAndroid Build Coastguard Worker (?<!\$) # Look-back: must not be preceded by another $. 49*795d594fSAndroid Build Coastguard Worker \$ 50*795d594fSAndroid Build Coastguard Worker (\{)? # May be enclosed by { } pair. 51*795d594fSAndroid Build Coastguard Worker (?P<name>\w+) # Save the symbol in named group. 52*795d594fSAndroid Build Coastguard Worker (?(1)\}) # Expect } if and only if { was present. 53*795d594fSAndroid Build Coastguard Worker''', re.VERBOSE) 54*795d594fSAndroid Build Coastguard Worker 55*795d594fSAndroid Build Coastguard Workerdef generate_script(output_filename, input_filenames): 56*795d594fSAndroid Build Coastguard Worker # Create new python script and write the initial setup code. 57*795d594fSAndroid Build Coastguard Worker script = StringIO() # File-like in-memory buffer. 58*795d594fSAndroid Build Coastguard Worker script.write("# DO NOT EDIT: This file was generated by gen-mterp.py.\n") 59*795d594fSAndroid Build Coastguard Worker script.write(open(SCRIPT_SETUP_CODE, "r").read()) 60*795d594fSAndroid Build Coastguard Worker script.write("def opcodes():\n") 61*795d594fSAndroid Build Coastguard Worker for i, opcode in enumerate(getOpcodeList()): 62*795d594fSAndroid Build Coastguard Worker script.write(' write_opcode({0}, "{1}", {1})\n'.format(i, opcode)) 63*795d594fSAndroid Build Coastguard Worker 64*795d594fSAndroid Build Coastguard Worker # Read all template files and translate them into python code. 65*795d594fSAndroid Build Coastguard Worker for input_filename in sorted(input_filenames): 66*795d594fSAndroid Build Coastguard Worker lines = open(input_filename, "r").readlines() 67*795d594fSAndroid Build Coastguard Worker indent = "" 68*795d594fSAndroid Build Coastguard Worker for line_number, line in enumerate(lines, 1): 69*795d594fSAndroid Build Coastguard Worker file_line = "{}:{}".format("/".join(input_filename.split("/")[-2:]), line_number) 70*795d594fSAndroid Build Coastguard Worker line = line.rstrip() 71*795d594fSAndroid Build Coastguard Worker if line.startswith("%"): 72*795d594fSAndroid Build Coastguard Worker script.write("{:80} # {}\n".format(line.lstrip("%"), file_line)) 73*795d594fSAndroid Build Coastguard Worker indent = indent_re.match(line).group(1) 74*795d594fSAndroid Build Coastguard Worker if line.endswith(":"): 75*795d594fSAndroid Build Coastguard Worker indent += " " 76*795d594fSAndroid Build Coastguard Worker else: 77*795d594fSAndroid Build Coastguard Worker line = escape_re.sub(r"''' + \g<name> + '''", line) 78*795d594fSAndroid Build Coastguard Worker line = line.replace("\\", "\\\\") 79*795d594fSAndroid Build Coastguard Worker line = line.replace("$$", "$") 80*795d594fSAndroid Build Coastguard Worker script.write(indent + "write_line('''" + line + "''')\n") 81*795d594fSAndroid Build Coastguard Worker script.write("\n") 82*795d594fSAndroid Build Coastguard Worker 83*795d594fSAndroid Build Coastguard Worker script.write("generate('''" + output_filename + "''')\n") 84*795d594fSAndroid Build Coastguard Worker script.seek(0) 85*795d594fSAndroid Build Coastguard Worker return script.read() 86*795d594fSAndroid Build Coastguard Worker 87*795d594fSAndroid Build Coastguard Workerif len(sys.argv) <= 3: 88*795d594fSAndroid Build Coastguard Worker print("Usage: output_file input_file(s)") 89*795d594fSAndroid Build Coastguard Worker sys.exit(1) 90*795d594fSAndroid Build Coastguard Worker 91*795d594fSAndroid Build Coastguard Worker# Generate the script and execute it. 92*795d594fSAndroid Build Coastguard Workeroutput_filename = sys.argv[1] 93*795d594fSAndroid Build Coastguard Workerinput_filenames = sys.argv[2:] 94*795d594fSAndroid Build Coastguard Workerscript_filename = output_filename + ".py" 95*795d594fSAndroid Build Coastguard Workerscript = generate_script(output_filename, input_filenames) 96*795d594fSAndroid Build Coastguard Workerwith open(script_filename, "w") as script_file: 97*795d594fSAndroid Build Coastguard Worker script_file.write(script) # Write to disk for debugging. 98*795d594fSAndroid Build Coastguard Workerexec(compile(script, script_filename, mode='exec')) 99