Skip to content

Commit d9ab47f

Browse files
committed
native/x64: 完善汇编解析和指令编码
1 parent e685476 commit d9ab47f

File tree

4 files changed

+171
-18
lines changed

4 files changed

+171
-18
lines changed

internal/native/parser/parser_file.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,35 @@ func (p *parser) parseFile() {
1616
// 解析开头的关联文档
1717
p.prog.Doc = p.parseCommentGroup(true)
1818

19+
// X64 强制采用 intel 语法
20+
if p.cpu == abi.X64Unix || p.cpu == abi.X64Windows {
21+
for {
22+
if p.err != nil {
23+
return
24+
}
25+
if p.tok == token.EOF {
26+
break
27+
}
28+
29+
switch p.tok {
30+
case token.COMMENT:
31+
commentObj := p.parseCommentGroup(true)
32+
p.prog.Comments = append(p.prog.Comments, commentObj)
33+
p.prog.Objects = append(p.prog.Objects, commentObj)
34+
35+
case token.GAS_X64_INTEL_SYNTAX:
36+
p.prog.IntelSyntax = &ast.GasIntelSyntaxNoprefix{
37+
Pos: p.pos,
38+
}
39+
p.acceptToken(token.GAS_X64_INTEL_SYNTAX)
40+
p.acceptToken(token.GAS_X64_NOPREFIX)
41+
}
42+
}
43+
if p.prog.IntelSyntax == nil {
44+
p.errorf(p.pos, "%s missing", token.GAS_X64_INTEL_SYNTAX)
45+
}
46+
}
47+
1948
// 解析代码主体
2049
for {
2150
if p.err != nil {
@@ -31,17 +60,6 @@ func (p *parser) parseFile() {
3160
p.prog.Comments = append(p.prog.Comments, commentObj)
3261
p.prog.Objects = append(p.prog.Objects, commentObj)
3362

34-
case token.GAS_X64_INTEL_SYNTAX:
35-
if p.cpu == abi.X64Unix || p.cpu == abi.X64Windows {
36-
p.prog.IntelSyntax = &ast.GasIntelSyntaxNoprefix{
37-
Pos: p.pos,
38-
}
39-
p.acceptToken(token.GAS_X64_INTEL_SYNTAX)
40-
p.acceptToken(token.GAS_X64_NOPREFIX)
41-
} else {
42-
p.errorf(p.pos, "%v only enabled on X64 CPU", p.tok)
43-
}
44-
4563
case token.GAS_EXTERN:
4664
ext := &ast.Extern{Pos: p.pos, Tok: token.GAS_EXTERN}
4765

internal/native/x64/lookup.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@ func RegValid(reg abi.RegType) bool {
1414
return reg > 0 && reg < REG_END
1515
}
1616

17+
func RegValid_Int(reg abi.RegType) bool {
18+
return RegValid_I8(reg) || RegValid_I16(reg) || RegValid_I32(reg) || RegValid_I64(64)
19+
}
20+
func RegValid_I8(reg abi.RegType) bool {
21+
return (reg >= REG_AL && reg <= REG_R15B) || (reg >= REG_AH && reg <= REG_BH)
22+
}
23+
func RegValid_I16(reg abi.RegType) bool {
24+
return reg >= REG_AX && reg <= REG_R15W
25+
}
26+
func RegValid_I32(reg abi.RegType) bool {
27+
return reg >= REG_EAX && reg <= REG_R15D
28+
}
29+
func RegValid_I64(reg abi.RegType) bool {
30+
return reg >= REG_RAX && reg <= REG_R15
31+
}
32+
33+
func RegValid_Float(reg abi.RegType) bool {
34+
return reg >= REG_XMM0 && reg <= REG_XMM7
35+
}
36+
1737
// 寄存器宽度
1838
func RegXLen(r abi.RegType) int {
1939
switch {

internal/native/x64/prog.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,18 @@ func (p *Prog) xLen(arg *abi.X64Argument) int {
5151

5252
func (p *Prog) reg2p9Reg(r abi.RegType) int16 {
5353
switch {
54+
case r == REG_RIP:
55+
return 0 // plan9 需要独处理
56+
case r >= REG_EAX && r <= REG_R15B:
57+
return p9x86.REG_AL + int16(r-REG_AL)
5458
case r >= REG_EAX && r <= REG_R15D:
5559
return p9x86.REG_AX + int16(r-REG_EAX)
5660
case r >= REG_RAX && r <= REG_R15:
5761
return p9x86.REG_AX + int16(r-REG_RAX)
62+
case r >= REG_AH && r <= REG_BH:
63+
return p9x86.REG_AH + int16(r-REG_AH)
64+
case r >= REG_XMM0 && r <= REG_XMM7:
65+
return p9x86.REG_F0 + int16(r-REG_XMM0)
5866
default:
5967
panic("unreachable")
6068
}

0 commit comments

Comments
 (0)