Esempio n. 1
0
	def compile(self, options):
		modules = Constants() # dict
		nextModules = set(['main'])
		debugData = {}

		for path in options.moduleSearchPath:
			if not os.path.isdir(path):
				raise CompilerError(None, None, "Module search path '%s' does not exist" % path)
				
		while nextModules:
			nextModule = nextModules.pop()
			if nextModule in modules:
				continue

			if options.verboseprogress:
				print 'Parsing module %s...' % nextModule

			modules[nextModule], foundModuleReferences = self.parse(options, nextModule)
			nextModules.update(foundModuleReferences)

		if options.verboseprogress:
			print 'Resolving identifiers...'
			
		self.resolveIdentifiers(modules)

		datafields, functions = self.merge(modules)

		self.registerIdentifiers(datafields, functions)

		self.verifyIdentifiers(datafields, functions)

		self.checkIdentifierUsage(datafields, functions)

		if options.optimize and False:
			if options.verboseprogress:
				print 'Optimizing source...'

			self.optimizeSyntaxTree(datafields, functions)

		print 'Max stack usage: %d bytes' % (functions['main.main'].stackUsage(functions) + 2)

		ihStackUsage = 0

		for function in functions:
			if function.startswith('interrupts.handler_'):
				ihStackUsage = max(ihStackUsage, functions[function].stackUsage(functions) + 2)

		if ihStackUsage:
			print 'Handling interrupts costs max %d bytes (%d with interrupted interrupts)' % (ihStackUsage, ihStackUsage * 2)

		if options.optimize:
			print 'Note: stack usage could be less, because optimization is enabled'

		if options.verboseprogress:
			print 'Generating assembly...'

		program, maxStackSize, internalMemoryEndAddress, externalMemoryEndAddress, newDebugData = asmgenerator.transform(datafields, functions)
		debugData.update(newDebugData)

		if options.optimize:
			if options.verboseprogress:
				print 'Optimizing assembly...'

			self.optimizeAsm(options, program)

		self.printAsmStatistics(program)

		self.checkCodeSize(program)

		print 'Stack space available: %d bytes' % maxStackSize

		if options.verboseinfo:
			if internalMemoryEndAddress >= 0x0E:
				print 'Internal memory used: 0x0E-0x%02X' % internalMemoryEndAddress

			if externalMemoryEndAddress >= 0x4000:
				print 'External memory used: 0x4000-0x%X' % externalMemoryEndAddress

		asm, newDebugData = asmgenerator.programToAsm(program, options)
		debugData.update(newDebugData)

		debugDataJson = json.dumps(debugData, allow_nan=False, indent=4)

		return asm, debugDataJson