1# 2# Copyright 2020, Data61, CSIRO (ABN 41 687 119 230) 3# 4# SPDX-License-Identifier: BSD-2-Clause 5# 6 7# these objects are to be filled in by the target 8 9import sys 10 11class TargetDir: 12 def __init__ (self): 13 self.d = None 14 def __str__ (self): 15 return self.d 16 def set_dir (self, d): 17 self.d = d 18 19target_dir = TargetDir () 20target_args = [] 21 22structs = {} 23functions = {} 24functions_by_tag = {} 25const_globals = {} 26 27symbols = {} 28sections = {} 29 30rodata = [None, None, None] 31 32pairings = {} 33# pre_pairings are optional 34pre_pairings = {} 35 36use_hooks = set () 37avail_hooks = {'problem_var_rep': {}, 'loop_var_analysis': {}, 38 'rep_unsafe_const_ret': {}, 'fun_calling_convention': {}, 39 'extra_wcet_assertions': {}, 'wcet_function_limits': {}, 40 'assume_sp_equal': {}, 'wcet_functions_to_avoid': {}, 41 'post_emit_node': {}, 42} 43 44def add_hook (hook_key, module_key, hook): 45 avail_hooks[hook_key][module_key] = hook 46def hooks (hook_key): 47 return [hook for (module_key, hook) 48 in avail_hooks[hook_key].iteritems () 49 if module_key in use_hooks] 50 51danger_set = set ([]) 52 53# this shared callback is used for tracing by everyone 54 55trace_depth = [0, 1] 56trace_files = [] 57 58def printout (s): 59 print s 60 sys.stdout.flush () 61 for f in trace_files: 62 f.write (s + '\n') 63 f.flush () 64 65def depth_tracer (s, push): 66 if push != 0: 67 trace_depth[0] += push 68 if trace_depth[0] <= trace_depth[1]: 69 printout (s) 70 71def default_tracer (s, push): 72 printout (s) 73 74tracer = [default_tracer] 75 76def trace (s, push = 0): 77 tracer[0](str (s), push) 78 79def load_target (target, target_args = None): 80 target_dir.set_dir (target) 81 if target_args != None: 82 target_args.extend (target_args) 83 package = '.'.join (__name__.split ('.')[:-1]) 84 if package: 85 pck = sys.modules[package] 86 pck.__path__.append (target) 87 else: 88 sys.path.append (target) 89 import target 90 91def load_target_args (args = None): 92 if args == None: 93 args = list (sys.argv) 94 if len (args) <= 1: 95 import os.path 96 objname = os.path.basename (args[0]) 97 dirname = os.path.dirname (args[0]) 98 exname = os.path.join (dirname, 'example') 99 print 'Usage: python %s <target> <instructions>' % objname 100 print 'Target should be a directory.' 101 if os.path.isdir (exname): 102 print 'See example target (in %s)' % exname 103 else: 104 print 'See example target in graph-refine dir.' 105 assert not 'Target specified' 106 else: 107 target = args[1] 108 t_args = [arg[7:] for arg in args 109 if arg.startswith ('target:')] 110 args = [arg for arg in args[2:] 111 if not arg.startswith ('target:')] 112 load_target (target, t_args) 113 return args 114 115