1 # 2 # Copyright (C) 2016 The Android Open Source Project 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 # 16 17 # Common global variables and helper methods for the in-memory python script. 18 # The script starts with this file and is followed by the code generated form 19 # the templated snippets. Those define all the helper functions used below. 20 21 import sys, re 22 from cStringIO import StringIO 23 24 out = StringIO() # File-like in-memory buffer. 25 handler_size_bytes = "MTERP_HANDLER_SIZE" 26 handler_size_bits = "MTERP_HANDLER_SIZE_LOG2" 27 opcode = "" 28 opnum = "" 29 30 def write_line(line): 31 out.write(line + "\n") 32 33 def balign(): 34 write_line(" .balign {}".format(handler_size_bytes)) 35 36 def write_opcode(num, name, write_method): 37 global opnum, opcode 38 opnum, opcode = str(num), name 39 write_line("/* ------------------------------ */") 40 balign() 41 write_line(".L_{1}: /* {0:#04x} */".format(num, name)) 42 opcode_start() 43 opcode_pre() 44 write_method() 45 opcode_end() 46 write_line("") 47 opnum, opcode = None, None 48 49 generated_helpers = {} 50 51 # This method generates a helper using the provided writer method. 52 # The output is temporarily redirected to in-memory buffer. 53 def add_helper(write_helper, name = None): 54 if name == None: 55 name = "mterp_" + opcode + "_helper" 56 global out 57 old_out = out 58 out = StringIO() 59 helper_start(name) 60 write_helper() 61 helper_end(name) 62 out.seek(0) 63 generated_helpers[name] = out.read() 64 out = old_out 65 return name 66 67 def generate(output_filename): 68 out.seek(0) 69 out.truncate() 70 write_line("/* DO NOT EDIT: This file was generated by gen-mterp.py. */") 71 header() 72 entry() 73 74 instruction_start() 75 opcodes() 76 balign() 77 instruction_end() 78 79 for name, helper in sorted(generated_helpers.items()): 80 out.write(helper) 81 helpers() 82 83 footer() 84 85 out.seek(0) 86 # Squash consequtive empty lines. 87 text = re.sub(r"(\n\n)(\n)+", r"\1", out.read()) 88 with open(output_filename, 'w') as output_file: 89 output_file.write(text) 90 91