1 # @file ConvertMasmToNasm.py
2 # This script assists with conversion of MASM assembly syntax to NASM
4 # Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>
6 # This program and the accompanying materials
7 # are licensed and made available under the terms and conditions of the BSD License
8 # which accompanies this distribution. The full text of the license may be found at
9 # http://opensource.org/licenses/bsd-license.php
11 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 from __future__
import print_function
28 class UnsupportedConversion(Exception):
32 class NoSourceFile(Exception):
36 class UnsupportedArch(Exception):
37 unsupported
= ('aarch64', 'arm', 'ebc', 'ipf')
42 # Version and Copyright
43 VersionNumber
= "0.01"
44 __version__
= "%prog Version " + VersionNumber
45 __copyright__
= "Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved."
46 __usage__
= "%prog [options] source.asm [destination.nasm]"
48 def __init__(self
, clone
=None):
50 self
.args
= self
.ProcessCommandLine()
52 self
.args
= clone
.args
54 self
.unsupportedSyntaxSeen
= False
55 self
.src
= self
.args
.source
56 assert(os
.path
.exists(self
.src
))
57 self
.dirmode
= os
.path
.isdir(self
.src
)
58 srcExt
= os
.path
.splitext(self
.src
)[1]
59 assert (self
.dirmode
or srcExt
!= '.nasm')
60 self
.infmode
= not self
.dirmode
and srcExt
== '.inf'
61 self
.diff
= self
.args
.diff
62 self
.git
= self
.args
.git
63 self
.force
= self
.args
.force
66 self
.rootdir
= os
.getcwd()
69 self
.rootdir
= clone
.rootdir
70 self
.gitdir
= clone
.gitdir
71 self
.gitemail
= clone
.gitemail
73 def ProcessCommandLine(self
):
74 parser
= argparse
.ArgumentParser(description
=self
.__copyright
__)
75 parser
.add_argument('--version', action
='version',
76 version
='%(prog)s ' + self
.VersionNumber
)
77 parser
.add_argument("-q", "--quiet", action
="store_true",
78 help="Disable all messages except FATAL ERRORS.")
79 parser
.add_argument("--git", action
="store_true",
80 help="Use git to create commits for each file converted")
81 parser
.add_argument("--diff", action
="store_true",
82 help="Show diff of conversion")
83 parser
.add_argument("-f", "--force", action
="store_true",
84 help="Force conversion even if unsupported")
85 parser
.add_argument('source', help='MASM input file')
86 parser
.add_argument('dest', nargs
='?',
87 help='NASM output file (default=input.nasm; - for stdout)')
89 return parser
.parse_args()
91 def RootRelative(self
, path
):
93 if result
.startswith(self
.rootdir
):
94 result
= result
[len(self
.rootdir
):]
95 while len(result
) > 0 and result
[0] in '/\\':
99 def MatchAndSetMo(self
, regexp
, string
):
100 self
.mo
= regexp
.match(string
)
101 return self
.mo
is not None
103 def SearchAndSetMo(self
, regexp
, string
):
104 self
.mo
= regexp
.search(string
)
105 return self
.mo
is not None
107 def ReplacePreserveSpacing(self
, string
, find
, replace
):
108 if len(find
) >= len(replace
):
109 padded
= replace
+ (' ' * (len(find
) - len(replace
)))
110 return string
.replace(find
, padded
)
111 elif find
.find(replace
) >= 0:
112 return string
.replace(find
, replace
)
114 lenDiff
= len(replace
) - len(find
)
116 for i
in range(lenDiff
, -1, -1):
117 padded
= find
+ (' ' * i
)
118 result
= result
.replace(padded
, replace
)
122 lastpath
= os
.path
.realpath(self
.src
)
125 path
= os
.path
.split(lastpath
)[0]
129 candidate
= os
.path
.join(path
, '.git')
130 if os
.path
.isdir(candidate
):
131 self
.gitdir
= candidate
132 self
.gitemail
= self
.FormatGitEmailAddress()
136 def FormatGitEmailAddress(self
):
137 if not self
.git
or not self
.gitdir
:
140 cmd
= ('git', 'config', 'user.name')
141 name
= self
.RunAndCaptureOutput(cmd
).strip()
142 cmd
= ('git', 'config', 'user.email')
143 email
= self
.RunAndCaptureOutput(cmd
).strip()
144 if name
.find(',') >= 0:
145 name
= '"' + name
+ '"'
146 return name
+ ' <' + email
+ '>'
148 def RunAndCaptureOutput(self
, cmd
, checkExitCode
=True, pipeIn
=None):
150 subpStdin
= subprocess
.PIPE
153 p
= subprocess
.Popen(args
=cmd
, stdout
=subprocess
.PIPE
, stdin
=subpStdin
)
154 (stdout
, stderr
) = p
.communicate(pipeIn
)
156 if p
.returncode
!= 0:
157 print('command:', ' '.join(cmd
))
158 print('stdout:', stdout
)
159 print('stderr:', stderr
)
160 print('return:', p
.returncode
)
161 assert p
.returncode
== 0
162 return stdout
.decode('utf-8', 'ignore')
164 def FileUpdated(self
, path
):
165 if not self
.git
or not self
.gitdir
:
168 cmd
= ('git', 'add', path
)
169 self
.RunAndCaptureOutput(cmd
)
171 def FileAdded(self
, path
):
172 self
.FileUpdated(path
)
174 def RemoveFile(self
, path
):
175 if not self
.git
or not self
.gitdir
:
178 cmd
= ('git', 'rm', path
)
179 self
.RunAndCaptureOutput(cmd
)
181 def FileConversionFinished(self
, pkg
, module
, src
, dst
):
182 if not self
.git
or not self
.gitdir
:
185 if not self
.args
.quiet
:
186 print('Committing: Conversion of', dst
)
188 prefix
= ' '.join(filter(lambda a
: a
, [pkg
, module
]))
190 if self
.unsupportedSyntaxSeen
:
192 message
+= '%s: Convert %s to NASM\n' % (prefix
, src
)
194 message
+= 'The %s script was used to convert\n' % sys
.argv
[0]
195 message
+= '%s to %s\n' % (src
, dst
)
197 message
+= 'Contributed-under: TianoCore Contribution Agreement 1.0\n'
198 assert(self
.gitemail
is not None)
199 message
+= 'Signed-off-by: %s\n' % self
.gitemail
200 message
= message
.encode('utf-8', 'ignore')
202 cmd
= ('git', 'commit', '-F', '-')
203 self
.RunAndCaptureOutput(cmd
, pipeIn
=message
)
206 class ConvertAsmFile(CommonUtils
):
208 def __init__(self
, src
, dst
, clone
):
209 CommonUtils
.__init
__(self
, clone
)
210 self
.ConvertAsmFile(src
, dst
)
214 def ConvertAsmFile(self
, inputFile
, outputFile
=None):
216 self
.unsupportedSyntaxSeen
= False
217 self
.inputFilename
= inputFile
219 outputFile
= os
.path
.splitext(inputFile
)[0] + '.nasm'
220 self
.outputFilename
= outputFile
222 fullSrc
= os
.path
.realpath(inputFile
)
223 srcParentDir
= os
.path
.basename(os
.path
.split(fullSrc
)[0])
224 maybeArch
= srcParentDir
.lower()
225 if maybeArch
in UnsupportedArch
.unsupported
:
226 raise UnsupportedArch
227 self
.ia32
= maybeArch
== 'ia32'
228 self
.x64
= maybeArch
== 'x64'
230 self
.inputFileBase
= os
.path
.basename(self
.inputFilename
)
231 self
.outputFileBase
= os
.path
.basename(self
.outputFilename
)
232 self
.output
= io
.BytesIO()
233 if not self
.args
.quiet
:
234 dirpath
, src
= os
.path
.split(self
.inputFilename
)
235 dirpath
= self
.RootRelative(dirpath
)
236 dst
= os
.path
.basename(self
.outputFilename
)
237 print('Converting:', dirpath
, src
, '->', dst
)
238 lines
= io
.open(self
.inputFilename
).readlines()
240 if self
.outputFilename
== '-' and not self
.diff
:
241 output_data
= self
.output
.getvalue()
242 if sys
.version_info
>= (3, 0):
243 output_data
= output_data
.decode('utf-8', 'ignore')
244 sys
.stdout
.write(output_data
)
247 f
= io
.open(self
.outputFilename
, 'wb')
248 f
.write(self
.output
.getvalue())
252 endOfLineRe
= re
.compile(r
'''
255 re
.VERBOSE | re
.MULTILINE
257 begOfLineRe
= re
.compile(r
'''
263 def Convert(self
, lines
):
265 self
.anonLabelCount
= -1
267 self
.oldAsmEmptyLineCount
= 0
268 self
.newAsmEmptyLineCount
= 0
270 mo
= self
.begOfLineRe
.search(line
)
271 assert mo
is not None
272 self
.indent
= mo
.group()
273 lineWithoutBeginning
= line
[len(self
.indent
):]
274 mo
= self
.endOfLineRe
.search(lineWithoutBeginning
)
278 endOfLine
= mo
.group()
279 oldAsm
= line
[len(self
.indent
):len(line
) - len(endOfLine
)]
280 self
.originalLine
= line
.rstrip()
281 if line
.strip() == '':
282 self
.oldAsmEmptyLineCount
+= 1
283 self
.TranslateAsm(oldAsm
, endOfLine
)
284 if line
.strip() != '':
285 self
.oldAsmEmptyLineCount
= 0
287 procDeclRe
= re
.compile(r
'''
290 (?: \s+ NEAR | FAR )?
292 (?: \s+ (PUBLIC | PRIVATE) )?
293 (?: \s+ USES ( (?: \s+ \w[\w0-9]* )+ ) )?
296 re
.VERBOSE | re
.IGNORECASE
299 procEndRe
= re
.compile(r
'''
304 re
.VERBOSE | re
.IGNORECASE
307 varAndTypeSubRe
= r
' (?: [\w@][\w@0-9]* ) (?: \s* : \s* \w+ )? '
308 publicRe
= re
.compile(r
'''
310 ( %s (?: \s* , \s* %s )* )
312 ''' % (varAndTypeSubRe
, varAndTypeSubRe
),
313 re
.VERBOSE | re
.IGNORECASE
316 varAndTypeSubRe
= re
.compile(varAndTypeSubRe
, re
.VERBOSE | re
.IGNORECASE
)
318 macroDeclRe
= re
.compile(r
'''
323 re
.VERBOSE | re
.IGNORECASE
326 sectionDeclRe
= re
.compile(r
'''
331 re
.VERBOSE | re
.IGNORECASE
334 externRe
= re
.compile(r
'''
335 EXTE?RN \s+ (?: C \s+ )?
336 ([\w@][\w@0-9]*) \s* : \s* (\w+)
339 re
.VERBOSE | re
.IGNORECASE
342 externdefRe
= re
.compile(r
'''
343 EXTERNDEF \s+ (?: C \s+ )?
344 ([\w@][\w@0-9]*) \s* : \s* (\w+)
347 re
.VERBOSE | re
.IGNORECASE
350 protoRe
= re
.compile(r
'''
356 re
.VERBOSE | re
.IGNORECASE
359 defineDataRe
= re
.compile(r
'''
361 ( db | dw | dd | dq ) \s+
365 re
.VERBOSE | re
.IGNORECASE
368 equRe
= re
.compile(r
'''
369 ([\w@][\w@0-9]*) \s+ EQU \s+ (\S.*?)
372 re
.VERBOSE | re
.IGNORECASE
375 ignoreRe
= re
.compile(r
'''
387 re
.VERBOSE | re
.IGNORECASE
390 whitespaceRe
= re
.compile(r
'\s+', re
.MULTILINE
)
392 def TranslateAsm(self
, oldAsm
, endOfLine
):
393 assert(oldAsm
.strip() == oldAsm
)
395 endOfLine
= endOfLine
.replace(self
.inputFileBase
, self
.outputFileBase
)
397 oldOp
= oldAsm
.split()
405 self
.EmitAsmWithComment(oldAsm
, newAsm
, endOfLine
)
406 elif oldOp
in ('#include', ):
408 self
.EmitLine(oldAsm
+ endOfLine
)
409 elif oldOp
.lower() in ('end', 'title', 'text'):
411 self
.EmitAsmWithComment(oldAsm
, newAsm
, endOfLine
)
412 elif oldAsm
.lower() == '@@:':
413 self
.anonLabelCount
+= 1
414 self
.EmitLine(self
.anonLabel(self
.anonLabelCount
) + ':')
415 elif self
.MatchAndSetMo(self
.ignoreRe
, oldAsm
):
417 self
.EmitAsmWithComment(oldAsm
, newAsm
, endOfLine
)
418 elif oldAsm
.lower() == 'ret':
419 for i
in range(len(self
.uses
) - 1, -1, -1):
420 register
= self
.uses
[i
]
421 self
.EmitNewContent('pop ' + register
)
423 self
.EmitAsmWithComment(oldAsm
, newAsm
, endOfLine
)
425 elif oldOp
.lower() == 'lea':
426 newAsm
= self
.ConvertLea(oldAsm
)
427 self
.EmitAsmWithComment(oldAsm
, newAsm
, endOfLine
)
428 elif oldAsm
.lower() == 'end':
430 self
.EmitAsmWithComment(oldAsm
, newAsm
, endOfLine
)
432 elif self
.MatchAndSetMo(self
.equRe
, oldAsm
):
433 equ
= self
.mo
.group(1)
434 newAsm
= '%%define %s %s' % (equ
, self
.mo
.group(2))
435 self
.EmitAsmWithComment(oldAsm
, newAsm
, endOfLine
)
436 elif self
.MatchAndSetMo(self
.externRe
, oldAsm
) or \
437 self
.MatchAndSetMo(self
.protoRe
, oldAsm
):
438 extern
= self
.mo
.group(1)
439 self
.NewGlobal(extern
)
440 newAsm
= 'extern ' + extern
441 self
.EmitAsmWithComment(oldAsm
, newAsm
, endOfLine
)
442 elif self
.MatchAndSetMo(self
.externdefRe
, oldAsm
):
444 self
.EmitAsmWithComment(oldAsm
, newAsm
, endOfLine
)
445 elif self
.MatchAndSetMo(self
.macroDeclRe
, oldAsm
):
446 newAsm
= '%%macro %s 0' % self
.mo
.group(1)
447 self
.EmitAsmWithComment(oldAsm
, newAsm
, endOfLine
)
448 elif oldOp
.lower() == 'endm':
449 newAsm
= r
'%endmacro'
450 self
.EmitAsmWithComment(oldAsm
, newAsm
, endOfLine
)
451 elif self
.MatchAndSetMo(self
.sectionDeclRe
, oldAsm
):
452 name
= self
.mo
.group(1)
453 ty
= self
.mo
.group(2)
454 if ty
.lower() == 'section':
458 self
.EmitAsmWithComment(oldAsm
, newAsm
, endOfLine
)
459 elif self
.MatchAndSetMo(self
.procDeclRe
, oldAsm
):
460 proc
= self
.proc
= self
.mo
.group(1)
461 visibility
= self
.mo
.group(2)
462 if visibility
is None:
465 visibility
= visibility
.lower()
466 if visibility
!= 'private':
467 self
.NewGlobal(self
.proc
)
468 proc
= 'ASM_PFX(' + proc
+ ')'
469 self
.EmitNewContent('global ' + proc
)
471 self
.EmitAsmWithComment(oldAsm
, newAsm
, endOfLine
)
472 uses
= self
.mo
.group(3)
474 uses
= filter(None, uses
.split())
478 for register
in self
.uses
:
479 self
.EmitNewContent(' push ' + register
)
480 elif self
.MatchAndSetMo(self
.procEndRe
, oldAsm
):
482 self
.EmitAsmWithComment(oldAsm
, newAsm
, endOfLine
)
483 elif self
.MatchAndSetMo(self
.publicRe
, oldAsm
):
484 publics
= re
.findall(self
.varAndTypeSubRe
, self
.mo
.group(1))
485 publics
= map(lambda p
: p
.split(':')[0].strip(), publics
)
486 for i
in range(len(publics
) - 1):
488 self
.EmitNewContent('global ASM_PFX(%s)' % publics
[i
])
492 newAsm
= 'global ASM_PFX(%s)' % name
493 self
.EmitAsmWithComment(oldAsm
, newAsm
, endOfLine
)
494 elif self
.MatchAndSetMo(self
.defineDataRe
, oldAsm
):
495 name
= self
.mo
.group(1)
496 ty
= self
.mo
.group(2)
497 value
= self
.mo
.group(3)
500 newAsm
= '%s: %s %s' % (name
, ty
, value
)
501 newAsm
= self
.CommonConversions(newAsm
)
502 self
.EmitAsmWithComment(oldAsm
, newAsm
, endOfLine
)
504 newAsm
= self
.CommonConversions(oldAsm
)
505 self
.EmitAsmWithComment(oldAsm
, newAsm
, endOfLine
)
507 def NewGlobal(self
, name
):
508 regex
= re
.compile(r
'(?<![_\w\d])(?<!ASM_PFX\()(' + re
.escape(name
) +
510 self
.globals.add(regex
)
512 def ConvertAnonymousLabels(self
, oldAsm
):
514 anonLabel
= self
.anonLabel(self
.anonLabelCount
)
515 newAsm
= newAsm
.replace('@b', anonLabel
)
516 newAsm
= newAsm
.replace('@B', anonLabel
)
517 anonLabel
= self
.anonLabel(self
.anonLabelCount
+ 1)
518 newAsm
= newAsm
.replace('@f', anonLabel
)
519 newAsm
= newAsm
.replace('@F', anonLabel
)
522 def anonLabel(self
, count
):
525 def EmitString(self
, string
):
526 self
.output
.write(string
.encode('utf-8', 'ignore'))
528 def EmitLineWithDiff(self
, old
, new
):
529 newLine
= (self
.indent
+ new
).rstrip()
532 print('+%s' % newLine
)
535 print('+%s' % newLine
)
539 self
.newAsmEmptyLineCount
= 0
540 self
.EmitString(newLine
+ '\r\n')
542 def EmitLine(self
, string
):
543 self
.EmitLineWithDiff(self
.originalLine
, string
)
545 def EmitNewContent(self
, string
):
546 self
.EmitLineWithDiff(None, string
)
548 def EmitAsmReplaceOp(self
, oldAsm
, oldOp
, newOp
, endOfLine
):
549 newAsm
= oldAsm
.replace(oldOp
, newOp
, 1)
550 self
.EmitAsmWithComment(oldAsm
, newAsm
, endOfLine
)
552 hexNumRe
= re
.compile(r
'0*((?=[\da-f])\d*(?<=\d)[\da-f]*)h', re
.IGNORECASE
)
554 def EmitAsmWithComment(self
, oldAsm
, newAsm
, endOfLine
):
555 for glblRe
in self
.globals:
556 newAsm
= glblRe
.sub(r
'ASM_PFX(\1)', newAsm
)
558 newAsm
= self
.hexNumRe
.sub(r
'0x\1', newAsm
)
560 newLine
= newAsm
+ endOfLine
561 emitNewLine
= ((newLine
.strip() != '') or
562 ((oldAsm
+ endOfLine
).strip() == ''))
563 if emitNewLine
and newLine
.strip() == '':
564 self
.newAsmEmptyLineCount
+= 1
565 if self
.newAsmEmptyLineCount
> 1:
568 self
.EmitLine(newLine
.rstrip())
570 print('-%s' % self
.originalLine
)
572 leaRe
= re
.compile(r
'''
573 (lea \s+) ([\w@][\w@0-9]*) \s* , \s* (\S (?:.*\S)?)
576 re
.VERBOSE | re
.IGNORECASE
579 def ConvertLea(self
, oldAsm
):
581 if self
.MatchAndSetMo(self
.leaRe
, oldAsm
):
582 lea
= self
.mo
.group(1)
583 dst
= self
.mo
.group(2)
584 src
= self
.mo
.group(3)
585 if src
.find('[') < 0:
586 src
= '[' + src
+ ']'
587 newAsm
= lea
+ dst
+ ', ' + src
588 newAsm
= self
.CommonConversions(newAsm
)
591 ptrRe
= re
.compile(r
'''
593 ([dfq]?word|byte) \s+ (?: ptr ) (\s*)
596 re
.VERBOSE | re
.IGNORECASE
599 def ConvertPtr(self
, oldAsm
):
601 while self
.SearchAndSetMo(self
.ptrRe
, newAsm
):
602 ty
= self
.mo
.group(1)
603 if ty
.lower() == 'fword':
606 ty
+= self
.mo
.group(2)
607 newAsm
= newAsm
[:self
.mo
.start(0)] + ty
+ newAsm
[self
.mo
.end(0):]
610 labelByteRe
= re
.compile(r
'''
611 (?: \s+ label \s+ (?: [dfq]?word | byte ) )
614 re
.VERBOSE | re
.IGNORECASE
617 def ConvertLabelByte(self
, oldAsm
):
619 if self
.SearchAndSetMo(self
.labelByteRe
, newAsm
):
620 newAsm
= newAsm
[:self
.mo
.start(0)] + ':' + newAsm
[self
.mo
.end(0):]
623 unaryBitwiseOpRe
= re
.compile(r
'''
627 re
.VERBOSE | re
.IGNORECASE
629 binaryBitwiseOpRe
= re
.compile(r
'''
631 ( AND | OR | SHL | SHR )
634 re
.VERBOSE | re
.IGNORECASE
636 bitwiseOpReplacements
= {
644 def ConvertBitwiseOp(self
, oldAsm
):
646 while self
.SearchAndSetMo(self
.binaryBitwiseOpRe
, newAsm
):
647 prefix
= self
.mo
.group(1)
648 op
= self
.bitwiseOpReplacements
[self
.mo
.group(2).lower()]
649 newAsm
= newAsm
[:self
.mo
.start(0)] + prefix
+ op
+ \
650 newAsm
[self
.mo
.end(0):]
651 while self
.SearchAndSetMo(self
.unaryBitwiseOpRe
, newAsm
):
652 op
= self
.bitwiseOpReplacements
[self
.mo
.group(1).lower()]
653 newAsm
= newAsm
[:self
.mo
.start(0)] + op
+ newAsm
[self
.mo
.end(0):]
656 sectionRe
= re
.compile(r
'''
663 re
.VERBOSE | re
.IGNORECASE
666 segmentRe
= re
.compile(r
'''
673 re
.VERBOSE | re
.IGNORECASE
676 def ConvertSection(self
, oldAsm
):
678 if self
.MatchAndSetMo(self
.sectionRe
, newAsm
) or \
679 self
.MatchAndSetMo(self
.segmentRe
, newAsm
):
680 name
= self
.mo
.group(1).lower()
683 self
.EmitLine('DEFAULT REL')
685 newAsm
= 'SECTION .' + name
688 fwordRe
= re
.compile(r
'''
693 re
.VERBOSE | re
.IGNORECASE
696 def FwordUnsupportedCheck(self
, oldAsm
):
698 if self
.SearchAndSetMo(self
.fwordRe
, newAsm
):
699 newAsm
= self
.Unsupported(newAsm
, 'fword used')
702 __common_conversion_routines__
= (
703 ConvertAnonymousLabels
,
705 FwordUnsupportedCheck
,
711 def CommonConversions(self
, oldAsm
):
713 for conv
in self
.__common
_conversion
_routines
__:
714 newAsm
= conv(self
, newAsm
)
717 def Unsupported(self
, asm
, message
=None):
719 raise UnsupportedConversion
721 self
.unsupportedSyntaxSeen
= True
722 newAsm
= '%error conversion unsupported'
724 newAsm
+= '; ' + message
729 class ConvertInfFile(CommonUtils
):
731 def __init__(self
, inf
, clone
):
732 CommonUtils
.__init
__(self
, clone
)
734 self
.ScanInfAsmFiles()
736 self
.ConvertInfAsmFiles()
738 infSrcRe
= re
.compile(r
'''
740 ( [\w@][\w@0-9/]* \.(asm|s) )
745 re
.VERBOSE | re
.IGNORECASE
748 def GetInfAsmFileMapping(self
):
749 srcToDst
= {'order': []}
750 for line
in self
.lines
:
752 if self
.MatchAndSetMo(self
.infSrcRe
, line
):
753 src
= self
.mo
.group(1)
754 srcExt
= self
.mo
.group(2)
755 dst
= os
.path
.splitext(src
)[0] + '.nasm'
756 if src
not in srcToDst
:
758 srcToDst
['order'].append(src
)
761 def ScanInfAsmFiles(self
):
763 assert os
.path
.isfile(src
)
764 f
= io
.open(src
, 'rt')
765 self
.lines
= f
.readlines()
768 path
= os
.path
.realpath(self
.inf
)
769 (self
.dir, inf
) = os
.path
.split(path
)
770 parent
= os
.path
.normpath(self
.dir)
771 (lastpath
, self
.moduleName
) = os
.path
.split(parent
)
772 self
.packageName
= None
774 lastpath
= os
.path
.normpath(lastpath
)
775 (parent
, basename
) = os
.path
.split(lastpath
)
776 if parent
== lastpath
:
778 if basename
.endswith('Pkg'):
779 self
.packageName
= basename
783 self
.srcToDst
= self
.GetInfAsmFileMapping()
785 self
.dstToSrc
= {'order': []}
786 for src
in self
.srcToDst
['order']:
787 srcExt
= os
.path
.splitext(src
)[1]
788 dst
= self
.srcToDst
[src
]
789 if dst
not in self
.dstToSrc
:
790 self
.dstToSrc
[dst
] = [src
]
791 self
.dstToSrc
['order'].append(dst
)
793 self
.dstToSrc
[dst
].append(src
)
796 return len(self
.dstToSrc
['order'])
799 return iter(self
.dstToSrc
['order'])
801 def ConvertInfAsmFiles(self
):
803 unsupportedArchCount
= 0
807 self
.UpdateInfAsmFile(dst
)
809 except UnsupportedConversion
:
810 if not self
.args
.quiet
:
811 print('MASM=>NASM conversion unsupported for', dst
)
812 notConverted
.append(dst
)
814 if not self
.args
.quiet
:
815 print('Source file missing for', reldst
)
816 notConverted
.append(dst
)
817 except UnsupportedArch
:
818 unsupportedArchCount
+= 1
821 self
.ConversionFinished(dst
)
822 if len(notConverted
) > 0 and not self
.args
.quiet
:
823 for dst
in notConverted
:
824 reldst
= self
.RootRelative(dst
)
825 print('Unabled to convert', reldst
)
826 if unsupportedArchCount
> 0 and not self
.args
.quiet
:
827 print('Skipped', unsupportedArchCount
, 'files based on architecture')
829 def UpdateInfAsmFile(self
, dst
, IgnoreMissingAsm
=False):
830 infPath
= os
.path
.split(os
.path
.realpath(self
.inf
))[0]
831 asmSrc
= os
.path
.splitext(dst
)[0] + '.asm'
832 fullSrc
= os
.path
.join(infPath
, asmSrc
)
833 fullDst
= os
.path
.join(infPath
, dst
)
834 srcParentDir
= os
.path
.basename(os
.path
.split(fullSrc
)[0])
835 if srcParentDir
.lower() in UnsupportedArch
.unsupported
:
836 raise UnsupportedArch
837 elif not os
.path
.exists(fullSrc
):
838 if not IgnoreMissingAsm
:
840 else: # not os.path.exists(fullDst):
841 conv
= ConvertAsmFile(fullSrc
, fullDst
, self
)
842 self
.unsupportedSyntaxSeen
= conv
.unsupportedSyntaxSeen
847 for line
in self
.lines
:
850 for src
in self
.dstToSrc
[dst
]:
851 assert self
.srcToDst
[src
] == dst
852 updatedLine
= self
.ReplacePreserveSpacing(
853 updatedLine
, src
, dst
)
855 lineChanged
= updatedLine
!= line
857 if lastLine
.strip() == updatedLine
.strip():
860 self
.lines
[i
] = updatedLine
+ '\n'
865 if self
.lines
[i
] is not None:
866 print('+%s' % updatedLine
)
870 fileChanged |
= lineChanged
871 if self
.lines
[i
] is not None:
872 lastLine
= self
.lines
[i
]
877 self
.lines
= list(filter(lambda l
: l
is not None, self
.lines
))
879 for src
in self
.dstToSrc
[dst
]:
880 if not src
.endswith('.asm'):
881 fullSrc
= os
.path
.join(infPath
, src
)
882 if os
.path
.exists(fullSrc
):
883 self
.RemoveFile(fullSrc
)
886 f
= io
.open(self
.inf
, 'w', newline
='\r\n')
887 f
.writelines(self
.lines
)
889 self
.FileUpdated(self
.inf
)
891 def ConversionFinished(self
, dst
):
892 asmSrc
= os
.path
.splitext(dst
)[0] + '.asm'
893 self
.FileConversionFinished(
894 self
.packageName
, self
.moduleName
, asmSrc
, dst
)
897 class ConvertInfFiles(CommonUtils
):
899 def __init__(self
, infs
, clone
):
900 CommonUtils
.__init
__(self
, clone
)
901 infs
= map(lambda i
: ConvertInfFile(i
, self
), infs
)
902 infs
= filter(lambda i
: len(i
) > 0, infs
)
903 dstToInfs
= {'order': []}
906 fulldst
= os
.path
.realpath(os
.path
.join(inf
.dir, dst
))
908 if fulldst
in dstToInfs
:
909 dstToInfs
[fulldst
].append(pair
)
911 dstToInfs
['order'].append(fulldst
)
912 dstToInfs
[fulldst
] = [pair
]
915 unsupportedArchCount
= 0
916 for dst
in dstToInfs
['order']:
919 for inf
, reldst
in dstToInfs
[dst
]:
920 inf
.UpdateInfAsmFile(reldst
, IgnoreMissingAsm
=didSomething
)
922 except UnsupportedConversion
:
923 if not self
.args
.quiet
:
924 print('MASM=>NASM conversion unsupported for', reldst
)
925 notConverted
.append(dst
)
927 if not self
.args
.quiet
:
928 print('Source file missing for', reldst
)
929 notConverted
.append(dst
)
930 except UnsupportedArch
:
931 unsupportedArchCount
+= 1
934 inf
.ConversionFinished(reldst
)
935 if len(notConverted
) > 0 and not self
.args
.quiet
:
936 for dst
in notConverted
:
937 reldst
= self
.RootRelative(dst
)
938 print('Unabled to convert', reldst
)
939 if unsupportedArchCount
> 0 and not self
.args
.quiet
:
940 print('Skipped', unsupportedArchCount
, 'files based on architecture')
943 class ConvertDirectories(CommonUtils
):
945 def __init__(self
, paths
, clone
):
946 CommonUtils
.__init
__(self
, clone
)
948 self
.ConvertInfAndAsmFiles()
950 def ConvertInfAndAsmFiles(self
):
952 for path
in self
.paths
:
953 assert(os
.path
.exists(path
))
954 for path
in self
.paths
:
955 for root
, dirs
, files
in os
.walk(path
):
956 for d
in ('.svn', '.git'):
960 if f
.lower().endswith('.inf'):
961 inf
= os
.path
.realpath(os
.path
.join(root
, f
))
964 ConvertInfFiles(infs
, self
)
967 class ConvertAsmApp(CommonUtils
):
970 CommonUtils
.__init
__(self
)
972 src
= self
.args
.source
975 ConvertInfFiles((src
,), self
)
977 ConvertDirectories((src
,), self
)
978 elif not self
.dirmode
:
979 ConvertAsmFile(src
, dst
, self
)