summaryrefslogtreecommitdiff
path: root/BaseTools/Scripts/ConvertMasmToNasm.py
blob: 7ad0bd23f3ee1a2a96329898c2fd1dbaef396d21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
# @file ConvertMasmToNasm.py
# This script assists with conversion of MASM assembly syntax to NASM
#
#  Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
#
#  This program and the accompanying materials
#  are licensed and made available under the terms and conditions of the BSD License
#  which accompanies this distribution.  The full text of the license may be found at
#  http://opensource.org/licenses/bsd-license.php
#
#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#

#
# Import Modules
#
import os.path
import re
import StringIO
import subprocess
import sys
from optparse import OptionParser


class UnsupportedConversion(Exception):
    pass


class NoSourceFile(Exception):
    pass


class UnsupportedArch(Exception):
    unsupported = ('aarch64', 'arm', 'ebc', 'ipf')


class CommonUtils:

    # Version and Copyright
    VersionNumber = "0.01"
    __version__ = "%prog Version " + VersionNumber
    __copyright__ = "Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved."
    __usage__ = "%prog [options] source.asm [destination.nasm]"

    def __init__(self, clone=None):
        if clone is None:
            (self.Opt, self.Args) = self.ProcessCommandLine()
        else:
            (self.Opt, self.Args) = (clone.Opt, clone.Args)

        self.unsupportedSyntaxSeen = False
        self.src = self.Args[0]
        assert(os.path.exists(self.src))
        self.dirmode = os.path.isdir(self.src)
        srcExt = os.path.splitext(self.src)[1]
        assert (self.dirmode or srcExt != '.nasm')
        self.infmode = not self.dirmode and srcExt == '.inf'
        self.diff = self.Opt.diff
        self.git = self.Opt.git
        self.force = self.Opt.force

        if clone is None:
            self.rootdir = os.getcwd()
            self.DetectGit()
        else:
            self.rootdir = clone.rootdir
            self.gitdir = clone.gitdir
            self.gitemail = clone.gitemail

    def ProcessCommandLine(self):
        Parser = OptionParser(description=self.__copyright__,
                              version=self.__version__,
                              prog=sys.argv[0],
                              usage=self.__usage__
                              )
        Parser.add_option("-q", "--quiet", action="store_true", type=None,
                          help="Disable all messages except FATAL ERRORS.")
        Parser.add_option("--git", action="store_true", type=None,
                          help="Use git to create commits for each file converted")
        Parser.add_option("--diff", action="store_true", type=None,
                          help="Show diff of conversion")
        Parser.add_option("-f", "--force", action="store_true", type=None,
                          help="Force conversion even if unsupported")

        (Opt, Args) = Parser.parse_args()

        if not Opt.quiet:
            print self.__copyright__
            Parser.print_version()

        return (Opt, Args)

    def RootRelative(self, path):
        result = path
        if result.startswith(self.rootdir):
            result = result[len(self.rootdir):]
            while len(result) > 0 and result[0] in '/\\':
                result = result[1:]
        return result

    def MatchAndSetMo(self, regexp, string):
        self.mo = regexp.match(string)
        return self.mo is not None

    def SearchAndSetMo(self, regexp, string):
        self.mo = regexp.search(string)
        return self.mo is not None

    def ReplacePreserveSpacing(self, string, find, replace):
        if len(find) >= len(replace):
            padded = replace + (' ' * (len(find) - len(replace)))
            return string.replace(find, padded)
        elif find.find(replace) >= 0:
            return string.replace(find, replace)
        else:
            lenDiff = len(replace) - len(find)
            result = string
            for i in range(lenDiff, -1, -1):
                padded = find + (' ' * i)
                result = result.replace(padded, replace)
            return result

    def DetectGit(self):
        lastpath = os.path.realpath(self.src)
        self.gitdir = None
        while True:
            path = os.path.split(lastpath)[0]
            if path == lastpath:
                return
            candidate = os.path.join(path, '.git')
            if os.path.isdir(candidate):
                self.gitdir = candidate
                self.gitemail = self.FormatGitEmailAddress()
                return
            lastpath = path

    def FormatGitEmailAddress(self):
        if not self.git or not self.gitdir:
            return ''

        cmd = ('git', 'config', 'user.name')
        name = self.RunAndCaptureOutput(cmd).strip()
        cmd = ('git', 'config', 'user.email')
        email = self.RunAndCaptureOutput(cmd).strip()
        if name.find(',') >= 0:
            name = '"' + name + '"'
        return name + ' <' + email + '>'

    def RunAndCaptureOutput(self, cmd, checkExitCode=True, pipeIn=None):
        if pipeIn:
            subpStdin = subprocess.PIPE
        else:
            subpStdin = None
        p = subprocess.Popen(args=cmd, stdout=subprocess.PIPE, stdin=subpStdin)
        (stdout, stderr) = p.communicate(pipeIn)
        if checkExitCode:
            if p.returncode != 0:
                print 'command:', ' '.join(cmd)
                print 'stdout:', stdout
                print 'stderr:', stderr
                print 'return:', p.returncode
            assert p.returncode == 0
        return stdout

    def FileUpdated(self, path):
        if not self.git or not self.gitdir:
            return

        cmd = ('git', 'add', path)
        self.RunAndCaptureOutput(cmd)

    def FileAdded(self, path):
        self.FileUpdated(path)

    def RemoveFile(self, path):
        if not self.git or not self.gitdir:
            return

        cmd = ('git', 'rm', path)
        self.RunAndCaptureOutput(cmd)

    def FileConversionFinished(self, pkg, module, src, dst):
        if not self.git or not self.gitdir:
            return

        if not self.Opt.quiet:
            print 'Committing: Conversion of', dst

        prefix = ' '.join(filter(lambda a: a, [pkg, module]))
        message = ''
        if self.unsupportedSyntaxSeen:
            message += 'ERROR! '
        message += '%s: Convert %s to NASM\n' % (prefix, src)
        message += '\n'
        message += 'The %s script was used to convert\n' % sys.argv[0]
        message += '%s to %s\n' % (src, dst)
        message += '\n'
        message += 'Contributed-under: TianoCore Contribution Agreement 1.0\n'
        message += 'Signed-off-by: %s\n' % self.gitemail

        cmd = ('git', 'commit', '-F', '-')
        self.RunAndCaptureOutput(cmd, pipeIn=message)


class ConvertAsmFile(CommonUtils):

    def __init__(self, src, dst, clone):
        CommonUtils.__init__(self, clone)
        self.ConvertAsmFile(src, dst)
        self.FileAdded(dst)
        self.RemoveFile(src)

    def ConvertAsmFile(self, inputFile, outputFile=None):
        self.globals = set()
        self.unsupportedSyntaxSeen = False
        self.inputFilename = inputFile
        if not outputFile:
            outputFile = os.path.splitext(inputFile)[0] + '.nasm'
        self.outputFilename = outputFile

        fullSrc = os.path.realpath(inputFile)
        srcParentDir = os.path.basename(os.path.split(fullSrc)[0])
        maybeArch = srcParentDir.lower()
        if maybeArch in UnsupportedArch.unsupported:
            raise UnsupportedArch
        self.ia32 = maybeArch == 'ia32'
        self.x64 = maybeArch == 'x64'

        self.inputFileBase = os.path.basename(self.inputFilename)
        self.outputFileBase = os.path.basename(self.outputFilename)
        if self.outputFilename == '-' and not self.diff:
            self.output = sys.stdout
        else:
            self.output = StringIO.StringIO()
        if not self.Opt.quiet:
            dirpath, src = os.path.split(self.inputFilename)
            dirpath = self.RootRelative(dirpath)
            dst = os.path.basename(self.outputFilename)
            print 'Converting:', dirpath, src, '->', dst
        lines = open(self.inputFilename).readlines()
        self.Convert(lines)
        if self.outputFilename == '-':
            if self.diff:
                sys.stdout.write(self.output.getvalue())
                self.output.close()
        else:
            f = open(self.outputFilename, 'wb')
            f.write(self.output.getvalue())
            f.close()
            self.output.close()

    endOfLineRe = re.compile(r'''
                                 \s* ( ; .* )? \n $
                             ''',
                             re.VERBOSE | re.MULTILINE
                             )
    begOfLineRe = re.compile(r'''
                                 \s*
                             ''',
                             re.VERBOSE
                             )

    def Convert(self, lines):
        self.proc = None
        self.anonLabelCount = -1
        output = self.output
        self.oldAsmEmptyLineCount = 0
        self.newAsmEmptyLineCount = 0
        for line in lines:
            mo = self.begOfLineRe.search(line)
            assert mo is not None
            self.indent = mo.group()
            lineWithoutBeginning = line[len(self.indent):]
            mo = self.endOfLineRe.search(lineWithoutBeginning)
            if mo is None:
                endOfLine = ''
            else:
                endOfLine = mo.group()
            oldAsm = line[len(self.indent):len(line) - len(endOfLine)]
            self.originalLine = line.rstrip()
            if line.strip() == '':
                self.oldAsmEmptyLineCount += 1
            self.TranslateAsm(oldAsm, endOfLine)
            if line.strip() != '':
                self.oldAsmEmptyLineCount = 0

    procDeclRe = re.compile(r'''
                                ([\w@][\w@0-9]*) \s+
                                PROC
                                (?: \s+ NEAR | FAR )?
                                (?: \s+ C )?
                                (?: \s+ (PUBLIC | PRIVATE) )?
                                (?: \s+ USES ( (?: \s+ \w[\w0-9]* )+ ) )?
                                \s* $
                            ''',
                            re.VERBOSE | re.IGNORECASE
                            )

    procEndRe = re.compile(r'''
                               ([\w@][\w@0-9]*) \s+
                               ENDP
                               \s* $
                           ''',
                           re.VERBOSE | re.IGNORECASE
                           )

    varAndTypeSubRe = r' (?: [\w@][\w@0-9]* ) (?: \s* : \s* \w+ )? '
    publicRe = re.compile(r'''
                              PUBLIC \s+
                              ( %s (?: \s* , \s* %s )* )
                              \s* $
                          ''' % (varAndTypeSubRe, varAndTypeSubRe),
                          re.VERBOSE | re.IGNORECASE
                          )

    varAndTypeSubRe = re.compile(varAndTypeSubRe, re.VERBOSE | re.IGNORECASE)

    macroDeclRe = re.compile(r'''
                                 ([\w@][\w@0-9]*) \s+
                                 MACRO
                                 \s* $
                             ''',
                             re.VERBOSE | re.IGNORECASE
                             )

    sectionDeclRe = re.compile(r'''
                                   ([\w@][\w@0-9]*) \s+
                                   ( SECTION | ENDS )
                                   \s* $
                               ''',
                               re.VERBOSE | re.IGNORECASE
                               )

    externRe = re.compile(r'''
                              EXTE?RN \s+ (?: C \s+ )?
                              ([\w@][\w@0-9]*) \s* : \s* (\w+)
                              \s* $
                           ''',
                          re.VERBOSE | re.IGNORECASE
                          )

    externdefRe = re.compile(r'''
                                 EXTERNDEF \s+ (?: C \s+ )?
                                 ([\w@][\w@0-9]*) \s* : \s* (\w+)
                                 \s* $
                             ''',
                             re.VERBOSE | re.IGNORECASE
                             )

    protoRe = re.compile(r'''
                             ([\w@][\w@0-9]*) \s+
                             PROTO
                             (?: \s+ .* )?
                             \s* $
                         ''',
                         re.VERBOSE | re.IGNORECASE
                         )

    defineDataRe = re.compile(r'''
                                  ([\w@][\w@0-9]*) \s+
                                  ( db | dw | dd | dq ) \s+
                                  ( .*? )
                                  \s* $
                              ''',
                              re.VERBOSE | re.IGNORECASE
                              )

    equRe = re.compile(r'''
                           ([\w@][\w@0-9]*) \s+ EQU \s+ (\S.*?)
                           \s* $
                       ''',
                       re.VERBOSE | re.IGNORECASE
                       )

    ignoreRe = re.compile(r'''
                              \. (?: const |
                                     mmx |
                                     model |
                                     xmm |
                                     x?list |
                                     [3-6]86p?
                                 ) |
                              page
                              (?: \s+ .* )?
                              \s* $
                          ''',
                          re.VERBOSE | re.IGNORECASE
                          )

    whitespaceRe = re.compile(r'\s+', re.MULTILINE)

    def TranslateAsm(self, oldAsm, endOfLine):
        assert(oldAsm.strip() == oldAsm)

        endOfLine = endOfLine.replace(self.inputFileBase, self.outputFileBase)

        oldOp = oldAsm.split()
        if len(oldOp) >= 1:
            oldOp = oldOp[0]
        else:
            oldOp = ''

        if oldAsm == '':
            newAsm = oldAsm
            self.EmitAsmWithComment(oldAsm, newAsm, endOfLine)
        elif oldOp in ('#include', ):
            newAsm = oldAsm
            self.EmitLine(oldAsm + endOfLine)
        elif oldOp.lower() in ('end', 'title', 'text'):
            newAsm = ''
            self.EmitAsmWithComment(oldAsm, newAsm, endOfLine)
        elif oldAsm.lower() == '@@:':
            self.anonLabelCount += 1
            self.EmitLine(self.anonLabel(self.anonLabelCount) + ':')
        elif self.MatchAndSetMo(self.ignoreRe, oldAsm):
            newAsm = ''
            self.EmitAsmWithComment(oldAsm, newAsm, endOfLine)
        elif oldAsm.lower() == 'ret':
            for i in range(len(self.uses) - 1, -1, -1):
                register = self.uses[i]
                self.EmitNewContent('pop     ' + register)
            newAsm = 'ret'
            self.EmitAsmWithComment(oldAsm, newAsm, endOfLine)
            self.uses = tuple()
        elif oldOp.lower() == 'lea':
            newAsm = self.ConvertLea(oldAsm)
            self.EmitAsmWithComment(oldAsm, newAsm, endOfLine)
        elif oldAsm.lower() == 'end':
            newAsm = ''
            self.EmitAsmWithComment(oldAsm, newAsm, endOfLine)
            self.uses = tuple()
        elif self.MatchAndSetMo(self.equRe, oldAsm):
            equ = self.mo.group(1)
            newAsm = '%%define %s %s' % (equ, self.mo.group(2))
            self.EmitAsmWithComment(oldAsm, newAsm, endOfLine)
        elif self.MatchAndSetMo(self.externRe, oldAsm) or \
                self.MatchAndSetMo(self.protoRe, oldAsm):
            extern = self.mo.group(1)
            self.NewGlobal(extern)
            newAsm = 'extern ' + extern
            self.EmitAsmWithComment(oldAsm, newAsm, endOfLine)
        elif self.MatchAndSetMo(self.externdefRe, oldAsm):
            newAsm = ''
            self.EmitAsmWithComment(oldAsm, newAsm, endOfLine)
        elif self.MatchAndSetMo(self.macroDeclRe, oldAsm):
            newAsm = '%%macro %s 0' % self.mo.group(1)
            self.EmitAsmWithComment(oldAsm, newAsm, endOfLine)
        elif oldOp.lower() == 'endm':
            newAsm = r'%endmacro'
            self.EmitAsmWithComment(oldAsm, newAsm, endOfLine)
        elif self.MatchAndSetMo(self.sectionDeclRe, oldAsm):
            name = self.mo.group(1)
            ty = self.mo.group(2)
            if ty.lower() == 'section':
                newAsm = '.' + name
            else:
                newAsm = ''
            self.EmitAsmWithComment(oldAsm, newAsm, endOfLine)
        elif self.MatchAndSetMo(self.procDeclRe, oldAsm):
            proc = self.proc = self.mo.group(1)
            visibility = self.mo.group(2)
            if visibility is None:
                visibility = ''
            else:
                visibility = visibility.lower()
            if visibility != 'private':
                self.NewGlobal(self.proc)
                proc = 'ASM_PFX(' + proc + ')'
                self.EmitNewContent('global ' + proc)
            newAsm = proc + ':'
            self.EmitAsmWithComment(oldAsm, newAsm, endOfLine)
            uses = self.mo.group(3)
            if uses is not None:
                uses = filter(None, uses.split())
            else:
                uses = tuple()
            self.uses = uses
            for register in self.uses:
                self.EmitNewContent('    push    ' + register)
        elif self.MatchAndSetMo(self.procEndRe, oldAsm):
            newAsm = ''
            self.EmitAsmWithComment(oldAsm, newAsm, endOfLine)
        elif self.MatchAndSetMo(self.publicRe, oldAsm):
            publics = re.findall(self.varAndTypeSubRe, self.mo.group(1))
            publics = map(lambda p: p.split(':')[0].strip(), publics)
            for i in range(len(publics) - 1):
                name = publics[i]
                self.EmitNewContent('global ASM_PFX(%s)' % publics[i])
                self.NewGlobal(name)
            name = publics[-1]
            self.NewGlobal(name)
            newAsm = 'global ASM_PFX(%s)' % name
            self.EmitAsmWithComment(oldAsm, newAsm, endOfLine)
        elif self.MatchAndSetMo(self.defineDataRe, oldAsm):
            name = self.mo.group(1)
            ty = self.mo.group(2)
            value = self.mo.group(3)
            if value == '?':
                value = 0
            newAsm = '%s: %s %s' % (name, ty, value)
            newAsm = self.CommonConversions(newAsm)
            self.EmitAsmWithComment(oldAsm, newAsm, endOfLine)
        else:
            newAsm = self.CommonConversions(oldAsm)
            self.EmitAsmWithComment(oldAsm, newAsm, endOfLine)

    def NewGlobal(self, name):
        regex = re.compile(r'(?<![_\w\d])(?<!ASM_PFX\()(' + re.escape(name) +
                           r')(?![_\w\d])')
        self.globals.add(regex)

    def ConvertAnonymousLabels(self, oldAsm):
        newAsm = oldAsm
        anonLabel = self.anonLabel(self.anonLabelCount)
        newAsm = newAsm.replace('@b', anonLabel)
        newAsm = newAsm.replace('@B', anonLabel)
        anonLabel = self.anonLabel(self.anonLabelCount + 1)
        newAsm = newAsm.replace('@f', anonLabel)
        newAsm = newAsm.replace('@F', anonLabel)
        return newAsm

    def anonLabel(self, count):
        return '.%d' % count

    def EmitString(self, string):
        self.output.write(string)

    def EmitLineWithDiff(self, old, new):
        newLine = (self.indent + new).rstrip()
        if self.diff:
            if old is None:
                print '+%s' % newLine
            elif newLine != old:
                print '-%s' % old
                print '+%s' % newLine
            else:
                print '', newLine
        if newLine != '':
            self.newAsmEmptyLineCount = 0
        self.EmitString(newLine + '\r\n')

    def EmitLine(self, string):
        self.EmitLineWithDiff(self.originalLine, string)

    def EmitNewContent(self, string):
        self.EmitLineWithDiff(None, string)

    def EmitAsmReplaceOp(self, oldAsm, oldOp, newOp, endOfLine):
        newAsm = oldAsm.replace(oldOp, newOp, 1)
        self.EmitAsmWithComment(oldAsm, newAsm, endOfLine)

    hexNumRe = re.compile(r'0*((?=[\da-f])\d*(?<=\d)[\da-f]*)h', re.IGNORECASE)

    def EmitAsmWithComment(self, oldAsm, newAsm, endOfLine):
        for glblRe in self.globals:
            newAsm = glblRe.sub(r'ASM_PFX(\1)', newAsm)

        newAsm = self.hexNumRe.sub(r'0x\1', newAsm)

        newLine = newAsm + endOfLine
        emitNewLine = ((newLine.strip() != '') or
                       ((oldAsm + endOfLine).strip() == ''))
        if emitNewLine and newLine.strip() == '':
            self.newAsmEmptyLineCount += 1
            if self.newAsmEmptyLineCount > 1:
                emitNewLine = False
        if emitNewLine:
            self.EmitLine(newLine.rstrip())
        elif self.diff:
            print '-%s' % self.originalLine

    leaRe = re.compile(r'''
                           (lea \s+) ([\w@][\w@0-9]*) \s* , \s* (\S (?:.*\S)?)
                           \s* $
                       ''',
                       re.VERBOSE | re.IGNORECASE
                       )

    def ConvertLea(self, oldAsm):
        newAsm = oldAsm
        if self.MatchAndSetMo(self.leaRe, oldAsm):
            lea = self.mo.group(1)
            dst = self.mo.group(2)
            src = self.mo.group(3)
            if src.find('[') < 0:
                src = '[' + src + ']'
            newAsm = lea + dst + ', ' + src
        newAsm = self.CommonConversions(newAsm)
        return newAsm

    ptrRe = re.compile(r'''
                           (?<! \S )
                           ([dfq]?word|byte) \s+ (?: ptr ) (\s*)
                           (?= [[\s] )
                       ''',
                       re.VERBOSE | re.IGNORECASE
                       )

    def ConvertPtr(self, oldAsm):
        newAsm = oldAsm
        while self.SearchAndSetMo(self.ptrRe, newAsm):
            ty = self.mo.group(1)
            if ty.lower() == 'fword':
                ty = ''
            else:
                ty += self.mo.group(2)
            newAsm = newAsm[:self.mo.start(0)] + ty + newAsm[self.mo.end(0):]
        return newAsm

    labelByteRe = re.compile(r'''
                                 (?: \s+ label \s+ (?: [dfq]?word | byte ) )
                                 (?! \S )
                             ''',
                             re.VERBOSE | re.IGNORECASE
                             )

    def ConvertLabelByte(self, oldAsm):
        newAsm = oldAsm
        if self.SearchAndSetMo(self.labelByteRe, newAsm):
            newAsm = newAsm[:self.mo.start(0)] + ':' + newAsm[self.mo.end(0):]
        return newAsm

    unaryBitwiseOpRe = re.compile(r'''
                                      ( NOT )
                                      (?= \s+ \S )
                                  ''',
                                  re.VERBOSE | re.IGNORECASE
                                  )
    binaryBitwiseOpRe = re.compile(r'''
                                       ( \S \s+ )
                                       ( AND | OR | SHL | SHR )
                                       (?= \s+ \S )
                                   ''',
                                   re.VERBOSE | re.IGNORECASE
                                   )
    bitwiseOpReplacements = {
        'not': '~',
        'and': '&',
        'shl': '<<',
        'shr': '>>',
        'or': '|',
    }

    def ConvertBitwiseOp(self, oldAsm):
        newAsm = oldAsm
        while self.SearchAndSetMo(self.binaryBitwiseOpRe, newAsm):
            prefix = self.mo.group(1)
            op = self.bitwiseOpReplacements[self.mo.group(2).lower()]
            newAsm = newAsm[:self.mo.start(0)] + prefix + op + \
                newAsm[self.mo.end(0):]
        while self.SearchAndSetMo(self.unaryBitwiseOpRe, newAsm):
            op = self.bitwiseOpReplacements[self.mo.group(1).lower()]
            newAsm = newAsm[:self.mo.start(0)] + op + newAsm[self.mo.end(0):]
        return newAsm

    sectionRe = re.compile(r'''
                               \. ( code |
                                    data
                                  )
                               (?: \s+ .* )?
                               \s* $
                           ''',
                           re.VERBOSE | re.IGNORECASE
                           )

    segmentRe = re.compile(r'''
                               ( code |
                                 data )
                               (?: \s+ SEGMENT )
                               (?: \s+ .* )?
                               \s* $
                           ''',
                           re.VERBOSE | re.IGNORECASE
                           )

    def ConvertSection(self, oldAsm):
        newAsm = oldAsm
        if self.MatchAndSetMo(self.sectionRe, newAsm) or \
           self.MatchAndSetMo(self.segmentRe, newAsm):
            name = self.mo.group(1).lower()
            if name == 'code':
                if self.x64:
                    self.EmitLine('DEFAULT REL')
                name = 'text'
            newAsm = 'SECTION .' + name
        return newAsm

    fwordRe = re.compile(r'''
                             (?<! \S )
                             fword
                             (?! \S )
                         ''',
                         re.VERBOSE | re.IGNORECASE
                         )

    def FwordUnsupportedCheck(self, oldAsm):
        newAsm = oldAsm
        if self.SearchAndSetMo(self.fwordRe, newAsm):
            newAsm = self.Unsupported(newAsm, 'fword used')
        return newAsm

    __common_conversion_routines__ = (
        ConvertAnonymousLabels,
        ConvertPtr,
        FwordUnsupportedCheck,
        ConvertBitwiseOp,
        ConvertLabelByte,
        ConvertSection,
    )

    def CommonConversions(self, oldAsm):
        newAsm = oldAsm
        for conv in self.__common_conversion_routines__:
            newAsm = conv(self, newAsm)
        return newAsm

    def Unsupported(self, asm, message=None):
        if not self.force:
            raise UnsupportedConversion

        self.unsupportedSyntaxSeen = True
        newAsm = '%error conversion unsupported'
        if message:
            newAsm += '; ' + message
        newAsm += ': ' + asm
        return newAsm


class ConvertInfFile(CommonUtils):

    def __init__(self, inf, clone):
        CommonUtils.__init__(self, clone)
        self.inf = inf
        self.ScanInfAsmFiles()
        if self.infmode:
            self.ConvertInfAsmFiles()

    infSrcRe = re.compile(r'''
                              \s*
                              ( [\w@][\w@0-9/]* \.(asm|s) )
                              \s* (?: \| [^#]* )?
                              \s* (?: \# .* )?
                              $
                          ''',
                          re.VERBOSE | re.IGNORECASE
                          )

    def GetInfAsmFileMapping(self):
        srcToDst = {'order': []}
        for line in self.lines:
            line = line.rstrip()
            if self.MatchAndSetMo(self.infSrcRe, line):
                src = self.mo.group(1)
                srcExt = self.mo.group(2)
                dst = os.path.splitext(src)[0] + '.nasm'
                if src not in srcToDst:
                    srcToDst[src] = dst
                    srcToDst['order'].append(src)
        return srcToDst

    def ScanInfAsmFiles(self):
        src = self.inf
        assert os.path.isfile(src)
        f = open(src)
        self.lines = f.readlines()
        f.close()

        path = os.path.realpath(self.inf)
        (self.dir, inf) = os.path.split(path)
        parent = os.path.normpath(self.dir)
        (lastpath, self.moduleName) = os.path.split(parent)
        self.packageName = None
        while True:
            lastpath = os.path.normpath(lastpath)
            (parent, basename) = os.path.split(lastpath)
            if parent == lastpath:
                break
            if basename.endswith('Pkg'):
                self.packageName = basename
                break
            lastpath = parent

        self.srcToDst = self.GetInfAsmFileMapping()

        self.dstToSrc = {'order': []}
        for src in self.srcToDst['order']:
            srcExt = os.path.splitext(src)[1]
            dst = self.srcToDst[src]
            if dst not in self.dstToSrc:
                self.dstToSrc[dst] = [src]
                self.dstToSrc['order'].append(dst)
            else:
                self.dstToSrc[dst].append(src)

    def __len__(self):
        return len(self.dstToSrc['order'])

    def __iter__(self):
        return iter(self.dstToSrc['order'])

    def ConvertInfAsmFiles(self):
        notConverted = []
        unsupportedArchCount = 0
        for dst in self:
            didSomething = False
            fileChanged = self.UpdateInfAsmFile(dst)
            try:
                self.UpdateInfAsmFile(dst)
                didSomething = True
            except UnsupportedConversion:
                if not self.Opt.quiet:
                    print 'MASM=>NASM conversion unsupported for', dst
                notConverted.append(dst)
            except NoSourceFile:
                if not self.Opt.quiet:
                    print 'Source file missing for', reldst
                notConverted.append(dst)
            except UnsupportedArch:
                unsupportedArchCount += 1
            else:
                if didSomething:
                    self.ConversionFinished(dst)
        if len(notConverted) > 0 and not self.Opt.quiet:
            for dst in notConverted:
                reldst = self.RootRelative(dst)
                print 'Unabled to convert', reldst
        if unsupportedArchCount > 0 and not self.Opt.quiet:
            print 'Skipped', unsupportedArchCount, 'files based on architecture'

    def UpdateInfAsmFile(self, dst, IgnoreMissingAsm=False):
        infPath = os.path.split(os.path.realpath(self.inf))[0]
        asmSrc = os.path.splitext(dst)[0] + '.asm'
        fullSrc = os.path.join(infPath, asmSrc)
        fullDst = os.path.join(infPath, dst)
        srcParentDir = os.path.basename(os.path.split(fullSrc)[0])
        if srcParentDir.lower() in UnsupportedArch.unsupported:
            raise UnsupportedArch
        elif not os.path.exists(fullSrc):
            if not IgnoreMissingAsm:
                raise NoSourceFile
        else:  # not os.path.exists(fullDst):
            conv = ConvertAsmFile(fullSrc, fullDst, self)
            self.unsupportedSyntaxSeen = conv.unsupportedSyntaxSeen

        lastLine = ''
        fileChanged = False
        for i in range(len(self.lines)):
            line = self.lines[i].rstrip()
            updatedLine = line
            for src in self.dstToSrc[dst]:
                assert self.srcToDst[src] == dst
                updatedLine = self.ReplacePreserveSpacing(
                    updatedLine, src, dst)

            lineChanged = updatedLine != line
            if lineChanged:
                if lastLine.strip() == updatedLine.strip():
                    self.lines[i] = None
                else:
                    self.lines[i] = updatedLine + '\r\n'

            if self.diff:
                if lineChanged:
                    print '-%s' % line
                    if self.lines[i] is not None:
                        print '+%s' % updatedLine
                else:
                    print '', line

            fileChanged |= lineChanged
            if self.lines[i] is not None:
                lastLine = self.lines[i]

        if fileChanged:
            self.lines = filter(lambda l: l is not None, self.lines)

        for src in self.dstToSrc[dst]:
            if not src.endswith('.asm'):
                fullSrc = os.path.join(infPath, src)
                if os.path.exists(fullSrc):
                    self.RemoveFile(fullSrc)

        if fileChanged:
            f = open(self.inf, 'wb')
            f.writelines(self.lines)
            f.close()
            self.FileUpdated(self.inf)

    def ConversionFinished(self, dst):
        asmSrc = os.path.splitext(dst)[0] + '.asm'
        self.FileConversionFinished(
            self.packageName, self.moduleName, asmSrc, dst)


class ConvertInfFiles(CommonUtils):

    def __init__(self, infs, clone):
        CommonUtils.__init__(self, clone)
        infs = map(lambda i: ConvertInfFile(i, self), infs)
        infs = filter(lambda i: len(i) > 0, infs)
        dstToInfs = {'order': []}
        for inf in infs:
            for dst in inf:
                fulldst = os.path.realpath(os.path.join(inf.dir, dst))
                pair = (inf, dst)
                if fulldst in dstToInfs:
                    dstToInfs[fulldst].append(pair)
                else:
                    dstToInfs['order'].append(fulldst)
                    dstToInfs[fulldst] = [pair]

        notConverted = []
        unsupportedArchCount = 0
        for dst in dstToInfs['order']:
            didSomething = False
            try:
                for inf, reldst in dstToInfs[dst]:
                    inf.UpdateInfAsmFile(reldst, IgnoreMissingAsm=didSomething)
                    didSomething = True
            except UnsupportedConversion:
                if not self.Opt.quiet:
                    print 'MASM=>NASM conversion unsupported for', reldst
                notConverted.append(dst)
            except NoSourceFile:
                if not self.Opt.quiet:
                    print 'Source file missing for', reldst
                notConverted.append(dst)
            except UnsupportedArch:
                unsupportedArchCount += 1
            else:
                if didSomething:
                    inf.ConversionFinished(reldst)
        if len(notConverted) > 0 and not self.Opt.quiet:
            for dst in notConverted:
                reldst = self.RootRelative(dst)
                print 'Unabled to convert', reldst
        if unsupportedArchCount > 0 and not self.Opt.quiet:
            print 'Skipped', unsupportedArchCount, 'files based on architecture'


class ConvertDirectories(CommonUtils):

    def __init__(self, paths, clone):
        CommonUtils.__init__(self, clone)
        self.paths = paths
        self.ConvertInfAndAsmFiles()

    def ConvertInfAndAsmFiles(self):
        infs = list()
        for path in self.paths:
            assert(os.path.exists(path))
        for path in self.paths:
            for root, dirs, files in os.walk(path):
                for d in ('.svn', '.git'):
                    if d in dirs:
                        dirs.remove(d)
                for f in files:
                    if f.lower().endswith('.inf'):
                        inf = os.path.realpath(os.path.join(root, f))
                        infs.append(inf)

        ConvertInfFiles(infs, self)


class ConvertAsmApp(CommonUtils):

    def __init__(self):
        CommonUtils.__init__(self)

        numArgs = len(self.Args)
        assert(numArgs >= 1)
        if self.infmode:
            ConvertInfFiles(self.Args, self)
        elif self.dirmode:
            ConvertDirectories(self.Args, self)
        elif not self.dirmode:
            assert(numArgs <= 2)
            src = self.Args[0]
            if numArgs > 1:
                dst = self.Args[1]
            else:
                dst = None
            ConvertAsmFile(src, dst, self)

ConvertAsmApp()