comparison python/ppci/target/basetarget.py @ 342:86b02c98a717 devel

Moved target directory
author Windel Bouwman
date Sat, 01 Mar 2014 15:40:31 +0100
parents python/target/basetarget.py@4d204f6f7d4e
children b4882ff0ed06
comparison
equal deleted inserted replaced
341:4d204f6f7d4e 342:86b02c98a717
1 from ppci.asmnodes import ASymbol, AInstruction, ANumber
2 from ppci import CompilerError
3
4 """
5 Base classes for defining a target
6 """
7
8 # Machine code interface:
9 class Operand:
10 """ Single machine operand """
11 pass
12
13 # standard immediates:
14
15 class ImmBase:
16 def __init__(self, imm):
17 assert type(imm) is int
18 assert imm < self.Max()
19 self.imm = imm
20
21 @classmethod
22 def Max(cls):
23 return 2**cls.bits
24
25 @classmethod
26 def Create(cls, vop):
27 if type(vop) is ANumber and vop.number < cls.Max():
28 return cls(vop.number)
29
30
31 class Imm3(ImmBase):
32 bits = 3
33
34
35 class Imm7(ImmBase):
36 bits = 7
37
38
39 class Imm8(ImmBase):
40 bits = 8
41
42
43 class Imm32(ImmBase):
44 bits = 32
45
46
47 class Instruction:
48 """ Base instruction class """
49 def encode(self):
50 return bytes()
51
52 def relocations(self):
53 return []
54
55 def symbols(self):
56 return []
57
58
59 class Nop(Instruction):
60 """ Instruction that does nothing and has zero size """
61 def encode(self):
62 return bytes()
63
64
65 class PseudoInstruction(Instruction):
66 pass
67
68
69 class Label(PseudoInstruction):
70 def __init__(self, name):
71 self.name = name
72
73 def __repr__(self):
74 return '{}:'.format(self.name)
75
76 def symbols(self):
77 return [self.name]
78
79 @classmethod
80 def Create(cls, vop):
81 if type(vop) is ASymbol:
82 name = vop.name
83 return cls(name)
84
85
86 class Comment(PseudoInstruction):
87 def __init__(self, txt):
88 self.txt = txt
89
90 def encode(self):
91 return bytes()
92
93 def __repr__(self):
94 return '; {}'.format(self.txt)
95
96
97 class Alignment(PseudoInstruction):
98 def __init__(self, a):
99 self.align = a
100
101 def __repr__(self):
102 return 'ALIGN({})'.format(self.align)
103
104 def encode(self):
105 pad = []
106 # TODO
107 address = 0
108 while (address % self.align) != 0:
109 address += 1
110 pad.append(0)
111 return bytes(pad)
112
113
114 class DebugInfo(PseudoInstruction):
115 def __init__(self, i):
116 self.info = i
117
118 def __repr__(self):
119 return 'DebugInfo: {}'.format(self.info)
120
121
122 class Register(Operand):
123 def __init__(self, name):
124 self.name = name
125
126
127 class Target:
128 def __init__(self, name, desc=''):
129 self.name = name
130 self.desc = desc
131 self.registers = []
132 self.byte_sizes = {'int' : 4} # For front end!
133
134 # For assembler:
135 self.assembler_rules = []
136 self.asm_keywords = []
137
138 def add_keyword(self, kw):
139 self.asm_keywords.append(kw)
140
141 def add_instruction(self, rhs, f):
142 self.add_rule('instruction', rhs, f)
143
144 def add_rule(self, lhs, rhs, f):
145 self.assembler_rules.append((lhs, rhs, f))
146
147 def instruction(self, cls):
148 """ Decorator function that registers an instruction to this target """
149 self.addInstruction(cls)
150 return cls
151
152 def addInstruction(self, i):
153 pass
154