1
1
# -*- coding: utf-8 -*-
2
-
3
2
from .env import *
4
3
from amoco .arch .core import Formatter
4
+ from amoco .ui .render import Token , TokenListJoin , highlight
5
+
6
+ def mn (m ,pad = 8 ):
7
+ return [(Token .Mnemonic , m .lower ().ljust (pad ))]
5
8
6
9
def mnemo (i ,pad = 8 ):
7
10
m = i .mnemonic
8
11
if i .BW : m += '.B'
9
- return m . lower (). ljust ( pad )
12
+ return mn ( m )
10
13
11
- def mnemo_cond (i ):
12
- m = mnemo ( i , pad = 0 ).replace ('jcc' ,'j' )
14
+ def jump (i ):
15
+ m = i . mnemonic . lower ( ).replace ('jcc' ,'j' )
13
16
s = COND [i .cond ][0 ].split ('/' )[0 ]
14
- return (m + s ).lower ().ljust (8 )
17
+ l = mn (m + s )
18
+ adr = i .operands [0 ].value
19
+ if i .address is None :
20
+ l .append ((Token .Constant ,'.%+' % adr ))
21
+ else :
22
+ l .append ((Token .Address ,'*%s' % (i .address + adr + 2 )))
23
+ return l
15
24
16
25
def ops (i ):
17
26
s = []
@@ -20,72 +29,94 @@ def ops(i):
20
29
assert i .BW
21
30
o = o .x
22
31
if o ._is_reg :
23
- s .append (str (o ))
32
+ s .append (( Token . Register , str (o ) ))
24
33
elif o ._is_cst :
25
- s .append ('#%s' % o )
34
+ s .append (( Token . Constant , '#%s' % o ) )
26
35
else :
27
36
assert o ._is_mem
28
37
a = o .a .base
29
- if a ._is_reg :
38
+ disp = o .a .disp
39
+ if disp == 0 and a ._is_reg :
30
40
if i .misc ['autoinc' ] is a :
31
- s .append ('@%s+' % a )
41
+ s .append (( Token . Memory , '@%s+' % a ) )
32
42
else :
33
- s .append ('@%s' % a )
43
+ s .append (( Token . Memory , '@%s' % a ) )
34
44
elif a ._is_cst :
35
- s .append ('&%s' % a )
45
+ a .sf = False
46
+ s .append ((Token .Memory ,'&%s' % a ))
36
47
else :
37
- assert a ._is_eqn
38
- l ,r = a .l ,a .r
48
+ if a ._is_eqn :
49
+ l ,r = a .l ,a .r
50
+ else :
51
+ l ,r = a ,disp
39
52
if l == pc and i .address :
40
- s .append ('*%s' % (i .address + r ))
53
+ s .append (( Token . Address , '*%s' % (i .address + r ) ))
41
54
else :
42
- s .append ('%s(%s)' % (r ,l ))
43
- return ', ' . join ( s )
55
+ s .append (( Token . Memory , '%s(%s)' % (r ,l ) ))
56
+ return TokenListJoin ( ', ' , s )
44
57
45
58
MSP430_full_formats = {
46
59
'msp430_doubleop' : [mnemo , ops ],
47
60
'msp430_singleop' : [mnemo , ops ],
48
- 'msp430_jumps' : [mnemo_cond , ops ],
61
+ 'msp430_jumps' : [jump ],
49
62
}
50
63
51
64
MSP430_full = Formatter (MSP430_full_formats )
52
65
53
- def MSP430_synthetic (null ,i ):
54
- s = MSP430_full (i )
66
+ def MSP430_synthetic (null ,i , toks = False ):
67
+ s = MSP430_full (i , True )
55
68
if i .mnemonic == 'ADDC' and i .operands [0 ]== 0 :
56
- return s .replace ('addc' ,'adc' ).replace ('#0x0,' ,'' )
57
- if i .mnemonic == 'DADD' and i .operands [0 ]== 0 :
58
- return s .replace ('dadd' ,'dadc' ).replace ('#0x0,' ,'' )
59
- if i .mnemonic == 'CMP' and i .operands [0 ]== 0 :
60
- return s .replace ('cmp' ,'tst' ).replace ('#0x0,' ,'' )
61
- if i .mnemonic == 'MOV' :
69
+ s [0 ] = mn ('adc' )[0 ]
70
+ del s [1 :3 ]
71
+ elif i .mnemonic == 'DADD' and i .operands [0 ]== 0 :
72
+ s [0 ] = mn ('dadc' )[0 ]
73
+ del s [1 :3 ]
74
+ elif i .mnemonic == 'CMP' and i .operands [0 ]== 0 :
75
+ s [0 ] = mn ('tst' )[0 ]
76
+ del s [1 :3 ]
77
+ elif i .mnemonic == 'MOV' :
62
78
if i .operands [1 ] is pc :
63
- return s .replace ('mov' ,'br' ).replace (',pc' ,'' )
79
+ s [0 ] = mn ('br' )[0 ]
80
+ del s [2 :4 ]
64
81
elif i .operands [0 ]== 0 :
65
- return s .replace ('mov' ,'clr' ).replace ('#0x0,' ,'' )
82
+ s [0 ] = mn ('clr' )[0 ]
83
+ del s [1 :3 ]
66
84
elif i .misc ['autoinc' ] is sp :
67
- if i .operands [1 ] is pc : return 'ret'
68
- return s .replace ('mov' ,'pop' ).replace ('@sp+,' ,'' )
69
- if i .mnemonic == 'BIC' :
85
+ if i .operands [1 ] is pc :
86
+ s = mn ('ret' )
87
+ else :
88
+ s [0 ] = mn ('pop' )[0 ]
89
+ del s [1 :3 ]
90
+ elif i .mnemonic == 'BIC' :
70
91
if i .operands [1 ] is sr :
71
- if i .operands [0 ]== 1 : return 'clrc'
72
- if i .operands [0 ]== 4 : return 'clrn'
73
- if i .operands [0 ]== 2 : return 'clrz'
74
- if i .operands [0 ]== 8 : return 'dint'
75
- if i .mnemonic == 'BIS' :
92
+ m = None
93
+ if i .operands [0 ]== 1 : m = 'clrc'
94
+ elif i .operands [0 ]== 4 : m = 'clrn'
95
+ elif i .operands [0 ]== 2 : m = 'clrz'
96
+ elif i .operands [0 ]== 8 : m = 'dint'
97
+ if m : s = mn (m )
98
+ elif i .mnemonic == 'BIS' :
76
99
if i .operands [1 ] is sr :
77
- if i .operands [0 ]== 1 : return 'setc'
78
- if i .operands [0 ]== 4 : return 'setn'
79
- if i .operands [0 ]== 2 : return 'setz'
80
- if i .operands [0 ]== 8 : return 'eint'
81
- if i .mnemonic == 'SUB' and i .operands [0 ]== 1 :
82
- return s .replace ('sub' ,'dec' ).replace ('#0x1,' ,'' )
83
- if i .mnemonic == 'SUB' and i .operands [0 ]== 2 :
84
- return s .replace ('sub' ,'decd' ).replace ('#0x2,' ,'' )
85
- if i .mnemonic == 'ADD' and i .operands [0 ]== 1 :
86
- return s .replace ('add' ,'inc' ).replace ('#0x1,' ,'' )
87
- if i .mnemonic == 'ADD' and i .operands [0 ]== 2 :
88
- return s .replace ('add' ,'incd' ).replace ('#0x2,' ,'' )
89
- if i .mnemonic == 'XOR' and i .operands [0 ].signextend (16 ).value == - 1 :
90
- return 'inv ' + ops (i ).split (',' )[- 1 ].strip ()
91
- return s
100
+ m = None
101
+ if i .operands [0 ]== 1 : m = 'setc'
102
+ elif i .operands [0 ]== 4 : m = 'setn'
103
+ elif i .operands [0 ]== 2 : m = 'setz'
104
+ elif i .operands [0 ]== 8 : m = 'eint'
105
+ if m : s = mn (m )
106
+ elif i .mnemonic == 'SUB' and i .operands [0 ]== 1 :
107
+ s [0 ] = mn ('dec' )[0 ]
108
+ del s [1 :3 ]
109
+ elif i .mnemonic == 'SUB' and i .operands [0 ]== 2 :
110
+ s [0 ] = mn ('decd' )[0 ]
111
+ del s [1 :3 ]
112
+ elif i .mnemonic == 'ADD' and i .operands [0 ]== 1 :
113
+ s [0 ] = mn ('inc' )[0 ]
114
+ del s [1 :3 ]
115
+ elif i .mnemonic == 'ADD' and i .operands [0 ]== 2 :
116
+ s [0 ] = mn ('incd' )[0 ]
117
+ del s [1 :3 ]
118
+ elif i .mnemonic == 'XOR' and i .operands [0 ].signextend (16 ).value == - 1 :
119
+ s [0 ] = mn ('inv' )[0 ]
120
+ del s [1 :- 1 ]
121
+ if toks : return s
122
+ return highlight (s )
0 commit comments