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