[Wisp-cvs] wisp/users/dig tran.py,1.99,1.100
Status: Alpha
Brought to you by:
digg
From: <di...@us...> - 2003-05-17 13:21:03
|
Update of /cvsroot/wisp/wisp/users/dig In directory sc8-pr-cvs1:/tmp/cvs-serv21777 Modified Files: tran.py Log Message: introduced the interpret procedure Index: tran.py =================================================================== RCS file: /cvsroot/wisp/wisp/users/dig/tran.py,v retrieving revision 1.99 retrieving revision 1.100 diff -u -d -r1.99 -r1.100 --- tran.py 17 May 2003 12:59:14 -0000 1.99 +++ tran.py 17 May 2003 13:20:59 -0000 1.100 @@ -229,17 +229,22 @@ interpreter.current = interpreter.sections[name] return switcher - def __init__ (this, byte_order, sections = ['.text']): - this.sections = {} + def __init__ (this, byte_order, output = ['.text']): this.meaning = Interpreter.universal_meaning.copy() - for s in sections: - this.sections[s] = Linkie(byte_order) - this.meaning[s] = 'builtin', this.make_section_switcher(s), 0 + # if first argument is a linkie, all output goes to that linkie + if isinstance(byte_order, Linkie): + this.sections = None + this.current = byte_order + else: + this.sections = {} + for s in output: + this.sections[s] = Linkie(byte_order) + this.meaning[s] = 'builtin', this.make_section_switcher(s), 0 + this.current = this.sections[output[0]] this.register_names = {} reg = Register([], names = this.register_names) this.meaning['reg'] = reg this.register_names[reg] = 'reg' - this.current = this.sections[sections[0]] this.recordee = None this.regpaths = [reg.child()] # for generating new registers this.hanging = {} @@ -302,6 +307,32 @@ tok = this.toksrc.get_token() this.toksrc = None +def interpret (toksrc, target, verbose = 0): + if isinstance(toksrc, list): toksrc = Macro_Cursor(toksrc) + if isinstance(toksrc, str): toksrc = Lexer(toksrc) + + if isinstance(target, str): target = target.split() + + if isinstance(target, Linkie): + interp = Interpreter(target) + interp.run(toksrc, verbose = verbose) + if interp.regstack: + raise 'regstack not empty after finishing interpretation', \ + interp.regstack + return target + elif isinstance(target, list): + byte_order = target[0] + sections = target[1:] + if sections: + interp = Interpreter(byte_order = byte_order, output = sections) + interp.run(toksrc, verbose = verbose) + if interp.regstack: + raise 'regstack not empty after finishing interpretation', \ + interp.regstack + return map((lambda n: interp.sections[n]), target[1:]) + else: + return interpret(toksrc, Linkie(byte_order)) + def main (): from getopt import getopt import elf @@ -315,40 +346,23 @@ verbose = 0 format = 'elf' opts, args = getopt(sys.argv[1:], 'vo:b:f:', - ['verbose', 'output=', 'base=', 'format=', 'list-words']) + ['verbose', 'output=', 'base=', 'format=']) output_name = None - list_words = 0 base = None for opt, arg in opts: if opt in ('-v', '--verbose'): verbose = 1 if opt in ('-o', '--output'): output_name = arg if opt in ('-b', '--base'): base = string.atol(arg, 16) if opt in ('-f', '--format'): format = arg - if opt == '--list-words': list_words = 1 if output_name == None: output_name = default_output_names[format] - if list_words: - wd = {} - for w in interpreter.meaning.keys(): - s = w.find(' ') - if s != -1: w = w[:s] - wd[w] = 1 - w = wd.keys() - w.sort() - print 'Internally known words:' - print ' ', ' '.join(w) if len(args) != 1: raise 'Invalid argument count -- must be 1', args - interpreter.run(Lexer(args[0]), verbose = verbose) - interpreter.sections['.text'].dump(title = 'TEXT') - interpreter.sections['.data'].dump(title = 'DATA') - interpreter.sections['.bss'].dump(title = 'BSS') - if interpreter.regstack: - raise 'Regstack not empty after parsing ended', interpreter.regstack - argum = { - 'text': interpreter.sections['.text'], - 'data': interpreter.sections['.data'], - 'bss': interpreter.sections['.bss'], - } + text, data, bss = interpret(Lexer(args[0]), '< .text .data .bss', + verbose = verbose) + text.dump(title = 'TEXT') + data.dump(title = 'DATA') + bss.dump(title = 'BSS') + argum = {'text': text, 'data': data, 'bss': bss} if base <> None: argum['base_address'] = base if format == 'elf': argum['flags'] = 's' |