def _include(self, name): """Include given filename. The Forth code is directly executed.""" # put all data from include in one chapter. remember previous chapter # at restore it at the end self.doctree.push_state() self.doctree.chapter(name) if name not in self.included_files: for prefix in self.include_path: path = os.path.join(prefix, name) if os.path.exists(path): self.logger.info('processing include %s' % (name, )) self.interpret(rpn.words_in_file(name)) self.logger.info('done include %s' % (name, )) self.included_files.append(name) break else: # as fallback, check internal library too try: data = pkgutil.get_data('msp430.asm', 'forth/%s' % (name, )) except IOError: raise ValueError('file not found: %s' % (name, )) else: self.logger.info('processing include %s' % (name, )) self.interpret( rpn.words_in_string(data, name='forth/%s' % (name, ), include_newline=True)) self.logger.info('done include %s' % (name, )) self.included_files.append(name) self.doctree.pop_state() # restore previous chapter and section
def _include(self, name): """Include given filename. The Forth code is directly executed.""" # put all data from include in one chapter. remember previous chapter # at restore it at the end self.doctree.push_state() self.doctree.chapter(name) if name not in self.included_files: for prefix in self.include_path: path = os.path.join(prefix, name) if os.path.exists(path): self.logger.info('processing include %s' % (name,)) self.interpret(rpn.words_in_file(name)) self.logger.info('done include %s' % (name,)) self.included_files.append(name) break else: # as fallback, check internal library too try: data = pkgutil.get_data('msp430.asm', 'forth/%s' % (name,)) except IOError: raise ValueError('file not found: %s' % (name,)) else: self.logger.info('processing include %s' % (name,)) self.interpret(rpn.words_in_string(data, name='forth/%s' % (name,), include_newline=True)) self.logger.info('done include %s' % (name,)) self.included_files.append(name) self.doctree.pop_state() # restore previous chapter and section
def main(): import logging import argparse logging.basicConfig() parser = argparse.ArgumentParser(description="""\ If no input files are specified data is read from stdin. Output is in "TI-Text" format.""") group = parser.add_argument_group('Input') group.add_argument( 'INPUT', type=argparse.FileType('r'), nargs='+', default=['-']) group.add_argument( '-T', '--segmentfile', help='linker definition file', metavar='FILE', default=None) group.add_argument( '-m', '--mcu', help='name of the MCU (used to load memory map)', metavar='MCU', default='MSP430F1121') group = parser.add_argument_group('Output') group.add_argument( '-o', '--outfile', type=argparse.FileType('w'), help='name of the destination file', default='-', metavar='FILE') group.add_argument( '--mapfile', type=argparse.FileType('w'), help='write map file', metavar='FILE') parser.add_argument( '-v', '--verbose', action='count', dest='verbose', default=0, help='print status messages, can be given multiple times to increase messages') parser.add_argument( '--debug', action='store_true', default=False, help='print debug messages') parser.add_argument( '--symbols', help='read register names for given architecture (e.g. F1xx)', metavar='NAME') args = parser.parse_args() #~ print(args) if args.debug: logging.getLogger().setLevel(logging.DEBUG) elif args.verbose: logging.getLogger().setLevel(logging.INFO) else: logging.getLogger().setLevel(logging.WARN) if sys.version_info < (3, 0): # XXX make stderr unicode capable sys.stderr = codecs.getwriter("utf-8")(sys.stderr) instructions = [] for fileobj in args.INPUT: if args.verbose > 2: sys.stderr.write(u'reading file "{}"...\n'.format(fileobj.name)) instructions.append('reset') instructions.extend(['filename', fileobj.name]) try: instructions.extend(rpn.words_in_file(fileobj.name, fileobj=fileobj)) except IOError as e: sys.stderr.write('ld: {}: File not found\n'.format(fileobj.name)) sys.exit(1) linker = Linker(instructions) # load symbols if args.symbols is not None: all_peripherals = peripherals.load_internal(args.symbols) for peripheral in all_peripherals.peripherals.values(): for reg_name, register in peripheral.items(): if reg_name.startswith('__'): continue if '__address__' in register: linker.labels[register['__name__']] = register['__address__'] for value, name in register['__bits__'].items(): linker.labels[name] = value for value, name in register['__values__'].items(): linker.labels[name] = value if '__values__' in peripheral: for value, name in peripheral['__values__'].items(): linker.labels[name] = value # ========= load MCU definition ========= if args.verbose > 1: sys.stderr.write("Step 1: load segment descriptions.\n") # load the file and get the desired MCU description try: if args.segmentfile: mem_maps = mcu_definition_parser.load_from_file(args.segmentfile) else: mem_maps = mcu_definition_parser.load_internal() args.mcu = args.mcu.upper() # XXX hack segment_definitions = mcu_definition_parser.expand_definition(mem_maps, args.mcu) except Exception as msg: sys.stderr.write('ERROR loading segment descriptions: {}\n'.format(msg)) raise sys.exit(1) linker.segments_from_definition(segment_definitions) if args.verbose > 2: sys.stderr.write('Segments available:\n') linker.top_segment.print_tree(sys.stderr) # ========= Do the actual linking ========= try: if args.verbose > 1: sys.stderr.write("Step 2: generate machine code\n") sys.stderr.write(" Pass 1: determinate segment sizes.\n") linker.pass_one() if args.verbose > 1: sys.stderr.write(" Pass 2: calculate labels.\n") linker.pass_two() if args.verbose > 1: sys.stderr.write(" Pass 3: final output.\n") linker.pass_three() except LinkError as e: #~ if e.lineno is not None else '?' sys.stderr.write(u'{e.filename}:{e.lineno}: {e}\n'.format(e=e)) sys.exit(1) except rpn.RPNError as e: sys.stderr.write(u'{e.filename}:{e.lineno}: {e}\n'.format(e=e)) if args.debug and e.text: sys.stderr.write(u'{e.filename}:{e.lineno}: input line: {e.text!r}\n'.format(e=e)) if args.debug: raise sys.exit(1) # ========= Output final result ========= if args.verbose > 1: sys.stderr.write('Step 3: write machine code to file.\n') args.outfile.write(to_TI_Text(linker.segments)) if args.verbose > 1: sys.stderr.write('Labels:\n') labels = sorted(linker.labels.keys()) for i in labels: sys.stderr.write(u' {:<24} = 0x{:08x}\n'.format(i, linker.labels[i])) if args.mapfile: labels = [(v, k) for k, v in linker.labels.items()] labels.sort() for address, label in labels: args.mapfile.write(u'0x{:04x} {}\n'.format(address, label)) args.mapfile.close() if args.verbose: sys.stderr.write('Segments used:\n') linker.top_segment.sort_subsegments(by_address=True) linker.top_segment.print_tree(sys.stderr, hide_empty=True)
def main(): from optparse import OptionParser logging.basicConfig(level=logging.ERROR) parser = OptionParser(usage="""\ %prog [options] [FILE...]|-] If no input files are specified data is read from stdin.""" ) parser.add_option( "-o", "--outfile", dest="outfile", help="write outputs to given file", metavar="FILE", default=None) parser.add_option( "-v", "--verbose", action="store_true", dest="verbose", default=False, help="print status messages") parser.add_option( "--debug", action="store_true", dest="debug", default=False, help="print debug messages") parser.add_option( "-i", "--interactive", action="store_true", dest="interactive", default=False, help="interactive mode is started") parser.add_option("-D", "--define", action = "append", dest = "defines", metavar = "SYM[=VALUE]", default = [], help="define symbol") parser.add_option("-I", "--include-path", action = "append", dest = "include_paths", metavar = "PATH", default = [], help="Add directory to the search path list for includes") (options, args) = parser.parse_args() if options.debug: logging.getLogger().setLevel(logging.DEBUG) elif options.verbose: logging.getLogger().setLevel(logging.INFO) else: logging.getLogger().setLevel(logging.WARN) # prepare output if options.outfile is not None: out = codecs.open(options.outfile, 'w', 'utf-8') else: out = codecs.getwriter("utf-8")(sys.stdout) # XXX make stderr unicode capable sys.stderr = codecs.getwriter("utf-8")(sys.stderr) instructions = [] include_paths = [] for filename in args: if filename == '-': if options.verbose: sys.stderr.write(u'reading stdin...\n') instructions.extend(rpn.words_in_file('<stdin>', fileobj=sys.stdin, include_newline=True)) include_paths.append('.') else: if options.verbose: sys.stderr.write(u'reading file "%s"...\n'% filename) try: instructions.extend(rpn.words_in_file(filename, include_newline=True)) except IOError as e: sys.stderr.write('forth: %s: File not found\n' % (filename,)) sys.exit(1) include_paths.append(os.path.dirname(os.path.abspath(filename))) try: forth = Forth() forth.init() # default to source directory as include path forth.include_path = include_paths # extend include search path forth.include_path.extend(options.include_paths) # insert defined symbols for definition in options.defines: if '=' in definition: symbol, value = definition.split('=', 1) else: symbol, value = definition, '1' forth.namespace[symbol.lower()] = value # XXX inserted as string only #~ forth.doctree.chapter(filename) forth.interpret(iter(instructions)) forth.doctree.render(out) except rpn.RPNError as e: sys.stderr.write(u"%s:%s: %s\n" % (e.filename, e.lineno, e)) if options.debug and e.text: sys.stderr.write(u"%s:%s: input line was: %r\n" % (e.filename, e.lineno, e.text)) #~ if options.debug: raise sys.exit(1) finally: # enter interactive loop when desired if options.interactive: rpn.interpreter_loop(debug = options.debug, rpn_instance=forth)
def load_symbols(filename): """\ Load symbols from given filename. """ return parse_words(rpn.words_in_file(filename))
def main(): import os import logging from optparse import OptionParser logging.basicConfig() parser = OptionParser( usage="""\ %prog [options] [FILE...]|-] If no input files are specified data is read from stdin. Output is in "TI-Text" format.""" ) parser.add_option( "-o", "--outfile", dest="outfile", help="name of the resulting binary (TI-Text)", metavar="FILE", default=None ) parser.add_option( "-T", "--segmentfile", dest="segmentfile", help="linker definition file", metavar="FILE", default=None ) parser.add_option( "-m", "--mcu", dest="mcu_name", help="name of the MCU (used to load memory map)", metavar="MCU", default="MSP430F1121", ) parser.add_option("--mapfile", dest="mapfile", help="write map file", metavar="FILE") parser.add_option( "-v", "--verbose", action="count", dest="verbose", default=0, help="print status messages, gan be given multiple times to increase messages", ) parser.add_option("--debug", action="store_true", dest="debug", default=False, help="print debug messages") parser.add_option( "--symbols", dest="symbols", help="read register names for given architecture (e.g. F1xx)", metavar="NAME" ) (options, args) = parser.parse_args() if options.debug: logging.getLogger().setLevel(logging.DEBUG) elif options.verbose: logging.getLogger().setLevel(logging.INFO) else: logging.getLogger().setLevel(logging.WARN) # prepare output if options.outfile is not None: out = open(options.outfile, "wb") else: out = sys.stdout # XXX make stderr unicode capable sys.stderr = codecs.getwriter("utf-8")(sys.stderr) instructions = [] for filename in args: if filename == "-": if options.verbose > 2: sys.stderr.write(u"reading stdin...\n") instructions.append("reset") instructions.extend(["filename", "<stdin>"]) instructions.extend(sys.stdin.read().split()) else: if options.verbose > 2: sys.stderr.write(u'reading file "%s"...\n' % filename) instructions.append("reset") instructions.extend(["filename", filename]) try: instructions.extend(rpn.words_in_file(filename)) except IOError, e: sys.stderr.write("ld: %s: File not found\n" % (filename,)) sys.exit(1)
def main(): import os import logging from optparse import OptionParser logging.basicConfig() parser = OptionParser(usage="""\ %prog [options] [FILE...]|-] If no input files are specified data is read from stdin. Output is in "TI-Text" format.""" ) parser.add_option( "-o", "--outfile", dest="outfile", help="name of the resulting binary (TI-Text)", metavar="FILE", default=None) parser.add_option( "-T", "--segmentfile", dest="segmentfile", help="linker definition file", metavar="FILE", default=None) parser.add_option( "-m", "--mcu", dest="mcu_name", help="name of the MCU (used to load memory map)", metavar="MCU", default='MSP430F1121') parser.add_option( "--mapfile", dest="mapfile", help="write map file", metavar="FILE") parser.add_option( "-v", "--verbose", action="count", dest="verbose", default=0, help="print status messages, gan be given multiple times to increase messages") parser.add_option( "--debug", action="store_true", dest="debug", default=False, help="print debug messages") parser.add_option("--symbols", dest="symbols", help="read register names for given architecture (e.g. F1xx)", metavar="NAME") (options, args) = parser.parse_args() if options.debug: logging.getLogger().setLevel(logging.DEBUG) elif options.verbose: logging.getLogger().setLevel(logging.INFO) else: logging.getLogger().setLevel(logging.WARN) # prepare output if options.outfile is not None: out = open(options.outfile, "wb") else: out = sys.stdout # XXX make stderr unicode capable sys.stderr = codecs.getwriter("utf-8")(sys.stderr) instructions = [] for filename in args: if filename == '-': if options.verbose > 2: sys.stderr.write(u'reading stdin...\n') instructions.append('reset') instructions.extend(['filename', '<stdin>']) instructions.extend(sys.stdin.read().split()) else: if options.verbose > 2: sys.stderr.write(u'reading file "%s"...\n'% filename) instructions.append('reset') instructions.extend(['filename', filename]) try: instructions.extend(rpn.words_in_file(filename)) except IOError, e: sys.stderr.write('ld: %s: File not found\n' % (filename,)) sys.exit(1)
def main(): from optparse import OptionParser logging.basicConfig(level=logging.ERROR) parser = OptionParser(usage="""\ %prog [options] [FILE...]|-] If no input files are specified data is read from stdin.""") parser.add_option("-o", "--outfile", dest="outfile", help="write outputs to given file", metavar="FILE", default=None) parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="print status messages") parser.add_option("--debug", action="store_true", dest="debug", default=False, help="print debug messages") parser.add_option("-i", "--interactive", action="store_true", dest="interactive", default=False, help="interactive mode is started") parser.add_option("-D", "--define", action="append", dest="defines", metavar="SYM[=VALUE]", default=[], help="define symbol") parser.add_option( "-I", "--include-path", action="append", dest="include_paths", metavar="PATH", default=[], help="Add directory to the search path list for includes") (options, args) = parser.parse_args() if options.debug: logging.getLogger().setLevel(logging.DEBUG) elif options.verbose: logging.getLogger().setLevel(logging.INFO) else: logging.getLogger().setLevel(logging.WARN) # prepare output if options.outfile is not None: out = codecs.open(options.outfile, 'w', 'utf-8') else: out = codecs.getwriter("utf-8")(sys.stdout) # XXX make stderr unicode capable sys.stderr = codecs.getwriter("utf-8")(sys.stderr) instructions = [] include_paths = [] for filename in args: if filename == '-': if options.verbose: sys.stderr.write(u'reading stdin...\n') instructions.extend( rpn.words_in_file('<stdin>', fileobj=sys.stdin, include_newline=True)) include_paths.append('.') else: if options.verbose: sys.stderr.write(u'reading file "%s"...\n' % filename) try: instructions.extend( rpn.words_in_file(filename, include_newline=True)) except IOError as e: sys.stderr.write('forth: %s: File not found\n' % (filename, )) sys.exit(1) include_paths.append(os.path.dirname(os.path.abspath(filename))) try: forth = Forth() forth.init() # default to source directory as include path forth.include_path = include_paths # extend include search path forth.include_path.extend(options.include_paths) # insert defined symbols for definition in options.defines: if '=' in definition: symbol, value = definition.split('=', 1) else: symbol, value = definition, '1' forth.namespace[ symbol.lower()] = value # XXX inserted as string only #~ forth.doctree.chapter(filename) forth.interpret(iter(instructions)) forth.doctree.render(out) except rpn.RPNError as e: sys.stderr.write(u"%s:%s: %s\n" % (e.filename, e.lineno, e)) if options.debug and e.text: sys.stderr.write(u"%s:%s: input line was: %r\n" % (e.filename, e.lineno, e.text)) #~ if options.debug: raise sys.exit(1) finally: # enter interactive loop when desired if options.interactive: rpn.interpreter_loop(debug=options.debug, rpn_instance=forth)
def main(): import argparse logging.basicConfig(level=logging.ERROR) parser = argparse.ArgumentParser() parser.add_argument('FORTHFILE', nargs='*', help='name of the input file(s)') group = parser.add_argument_group('Input') group.add_argument( '-I', '--include-path', action='append', metavar="PATH", default=[], help='Add directory to the search path list for includes') group.add_argument('-D', '--define', action='append', dest='defines', metavar='SYM[=VALUE]', default=[], help='define symbol') group = parser.add_argument_group('Output') group.add_argument('-o', '--outfile', type=argparse.FileType('w'), default='-', help='name of the output file (default: %(default)s)', metavar="FILE") parser.add_argument('-i', '--interactive', action='store_true', default=False, help='interactive mode is started') parser.add_argument('-v', '--verbose', action='store_true', default=False, help='print status messages') parser.add_argument('--develop', action='store_true', default=False, help='print debug messages') args = parser.parse_args() if args.develop: logging.getLogger('cpp').setLevel(logging.DEBUG) elif args.verbose: logging.getLogger('cpp').setLevel(logging.INFO) else: logging.getLogger('cpp').setLevel(logging.WARN) if sys.version_info < (3, 0): # XXX make stderr unicode capable sys.stderr = codecs.getwriter("utf-8")(sys.stderr) instructions = [] include_paths = [] for filename in args.FORTHFILE: if filename == '-': if args.verbose: sys.stderr.write(u'reading stdin...\n') instructions.extend( rpn.words_in_file('<stdin>', fileobj=sys.stdin, include_newline=True)) include_paths.append('.') else: if args.verbose: sys.stderr.write(u'reading file "{}"...\n'.format(filename)) try: instructions.extend( rpn.words_in_file(filename, include_newline=True)) except IOError as e: sys.stderr.write( 'forth: {}: File not found\n'.format(filename)) sys.exit(1) include_paths.append(os.path.dirname(os.path.abspath(filename))) try: forth = Forth() forth.init() # default to source directory as include path forth.include_path = include_paths # extend include search path forth.include_path.extend(args.include_path) # insert defined symbols for definition in args.defines: if '=' in definition: symbol, value = definition.split('=', 1) else: symbol, value = definition, '1' forth.namespace[ symbol.lower()] = value # XXX inserted as string only #~ forth.doctree.chapter(filename) forth.interpret(iter(instructions)) forth.doctree.render(args.outfile) except rpn.RPNError as e: sys.stderr.write(u'{e.filename}:{e.lineno}: {e}\n'.format(e=e)) if args.develop and e.text: sys.stderr.write( u'{e.filename}:{e.lineno}: input line: {e.text!r}\n'.format( e=e)) #~ if args.develop: raise sys.exit(1) finally: # enter interactive loop when desired if args.interactive: rpn.interpreter_loop(debug=args.develop, rpn_instance=forth)