annotate python/ppci/target/x86/target_x86.py @ 398:c0d9837acde8

x86 target refactor
author Windel Bouwman
date Thu, 29 May 2014 12:13:37 +0200
parents 86b02c98a717
children
rev   line source
398
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
1 from ..basetarget import Target
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
2 from ...assembler import BaseAssembler
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
3 from .registers import rax, rcx, rdx, rbx, rsp, rbp, rsi, rdi
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
4 from .registers import r8, r9, r10, r11, r12, r13, r14, r15, regs64
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
5 from .instructions import Mov, Inc, Xor, Push, Pop
291
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
6
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
7
398
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
8 class X86Assembler(BaseAssembler):
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
9 def __init__(self, target):
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
10 super().__init__(target)
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
11 self.make_parser()
291
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
12
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
13
398
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
14 class X86Target(Target):
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
15 """ x86 target containing assembler, linker"""
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
16 def __init__(self):
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
17 super().__init__('x86')
291
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
18
398
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
19 for reg in regs64:
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
20 self.add_keyword(reg.name)
291
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
21
398
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
22 self.add_rule('reg', ['rax'], lambda rhs: rax)
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
23 self.add_rule('reg', ['rcx'], lambda rhs: rcx)
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
24 self.add_rule('reg', ['rdx'], lambda rhs: rdx)
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
25 self.add_rule('reg', ['rbx'], lambda rhs: rbx)
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
26 self.add_rule('reg', ['rsp'], lambda rhs: rsp)
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
27 self.add_rule('reg', ['rbp'], lambda rhs: rbp)
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
28 self.add_rule('reg', ['rsi'], lambda rhs: rsi)
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
29 self.add_rule('reg', ['rdi'], lambda rhs: rdi)
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
30 self.add_rule('reg', ['r8'], lambda rhs: r8)
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
31 self.add_rule('reg', ['r9'], lambda rhs: r9)
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
32 self.add_rule('reg', ['r10'], lambda rhs: r10)
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
33 self.add_rule('reg', ['r11'], lambda rhs: r11)
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
34 self.add_rule('reg', ['r12'], lambda rhs: r12)
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
35 self.add_rule('reg', ['r13'], lambda rhs: r13)
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
36 self.add_rule('reg', ['r14'], lambda rhs: r14)
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
37 self.add_rule('reg', ['r15'], lambda rhs: r15)
291
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
38
398
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
39 self.add_keyword('mov')
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
40 self.add_instruction(['mov', 'reg', ',', 'reg'],
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
41 lambda rhs: Mov(rhs[1], rhs[3]))
291
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
42
398
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
43 self.add_keyword('xor')
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
44 self.add_instruction(['xor', 'reg', ',', 'reg'],
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
45 lambda rhs: Xor(rhs[1], rhs[3]))
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
46
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
47 self.add_keyword('inc')
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
48 self.add_instruction(['inc', 'reg'],
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
49 lambda rhs: Inc(rhs[1]))
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
50
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
51 self.add_keyword('push')
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
52 self.add_instruction(['push', 'reg'],
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
53 lambda rhs: Push(rhs[1]))
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
54
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
55 self.add_keyword('pop')
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
56 self.add_instruction(['pop', 'reg'],
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
57 lambda rhs: Pop(rhs[1]))
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
58
c0d9837acde8 x86 target refactor
Windel Bouwman
parents: 342
diff changeset
59 self.assembler = X86Assembler(self)