Mercurial > lcfOS
annotate python/ppci/target/basetarget.py @ 360:42343d189e14
Bugfix in for loop
author | Windel Bouwman |
---|---|
date | Fri, 14 Mar 2014 16:11:32 +0100 |
parents | 5477e499b039 |
children | c05ab629976a |
rev | line source |
---|---|
200 | 1 from ppci import CompilerError |
199 | 2 |
3 """ | |
4 Base classes for defining a target | |
5 """ | |
6 | |
234 | 7 class Instruction: |
292 | 8 """ Base instruction class """ |
234 | 9 def encode(self): |
335 | 10 return bytes() |
292 | 11 |
335 | 12 def relocations(self): |
13 return [] | |
14 | |
15 def symbols(self): | |
16 return [] | |
234 | 17 |
18 | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
19 class Nop(Instruction): |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
20 """ Instruction that does nothing and has zero size """ |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
21 def encode(self): |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
22 return bytes() |
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
23 |
354 | 24 def __repr__(self): |
25 return 'NOP' | |
26 | |
280
02385f62f250
Rework from str interface to Instruction interface
Windel Bouwman
parents:
277
diff
changeset
|
27 |
234 | 28 class PseudoInstruction(Instruction): |
29 pass | |
30 | |
31 | |
32 class Label(PseudoInstruction): | |
206 | 33 def __init__(self, name): |
34 self.name = name | |
35 | |
235 | 36 def __repr__(self): |
37 return '{}:'.format(self.name) | |
38 | |
335 | 39 def symbols(self): |
40 return [self.name] | |
235 | 41 |
42 | |
234 | 43 class Comment(PseudoInstruction): |
44 def __init__(self, txt): | |
45 self.txt = txt | |
249 | 46 |
235 | 47 def encode(self): |
48 return bytes() | |
249 | 49 |
234 | 50 def __repr__(self): |
51 return '; {}'.format(self.txt) | |
52 | |
235 | 53 |
234 | 54 class Alignment(PseudoInstruction): |
55 def __init__(self, a): | |
56 self.align = a | |
235 | 57 |
58 def __repr__(self): | |
59 return 'ALIGN({})'.format(self.align) | |
60 | |
234 | 61 def encode(self): |
62 pad = [] | |
335 | 63 # TODO |
64 address = 0 | |
235 | 65 while (address % self.align) != 0: |
234 | 66 address += 1 |
67 pad.append(0) | |
68 return bytes(pad) | |
69 | |
292 | 70 |
249 | 71 class DebugInfo(PseudoInstruction): |
72 def __init__(self, i): | |
73 self.info = i | |
74 | |
75 def __repr__(self): | |
76 return 'DebugInfo: {}'.format(self.info) | |
77 | |
292 | 78 |
346 | 79 class Register: |
199 | 80 def __init__(self, name): |
81 self.name = name | |
82 | |
83 | |
84 class Target: | |
201 | 85 def __init__(self, name, desc=''): |
86 self.name = name | |
87 self.desc = desc | |
200 | 88 self.registers = [] |
341 | 89 self.byte_sizes = {'int' : 4} # For front end! |
354 | 90 self.byte_sizes['byte'] = 1 |
341 | 91 |
346 | 92 # For lowering: |
93 self.lower_functions = {} | |
94 | |
341 | 95 # For assembler: |
96 self.assembler_rules = [] | |
97 self.asm_keywords = [] | |
98 | |
346 | 99 self.generate_base_rules() |
100 | |
101 def generate_base_rules(self): | |
345 | 102 # Base rules for constants: |
103 self.add_rule('imm32', ['val32'], lambda x: x[0].val) | |
104 self.add_rule('imm32', ['imm16'], lambda x: x[0]) | |
105 | |
106 self.add_rule('imm16', ['val16'], lambda x: x[0].val) | |
107 self.add_rule('imm16', ['imm12'], lambda x: x[0]) | |
108 | |
109 self.add_rule('imm12', ['val12'], lambda x: x[0].val) | |
110 self.add_rule('imm12', ['imm8'], lambda x: x[0]) | |
111 | |
112 self.add_rule('imm8', ['val8'], lambda x: x[0].val) | |
113 self.add_rule('imm8', ['imm5'], lambda x: x[0]) | |
114 | |
115 self.add_rule('imm5', ['val5'], lambda x: x[0].val) | |
116 self.add_rule('imm5', ['imm3'], lambda x: x[0]) | |
117 | |
118 self.add_rule('imm3', ['val3'], lambda x: x[0].val) | |
119 | |
341 | 120 def add_keyword(self, kw): |
121 self.asm_keywords.append(kw) | |
122 | |
123 def add_instruction(self, rhs, f): | |
124 self.add_rule('instruction', rhs, f) | |
125 | |
126 def add_rule(self, lhs, rhs, f): | |
127 self.assembler_rules.append((lhs, rhs, f)) | |
200 | 128 |
346 | 129 def lower_frame_to_stream(self, frame, outs): |
130 """ Lower instructions from frame to output stream """ | |
131 for im in frame.instructions: | |
132 if isinstance(im.assem, Instruction): | |
133 outs.emit(im.assem) | |
134 else: | |
135 ins = self.lower_functions[im.assem](im) | |
136 outs.emit(ins) | |
201 | 137 |
346 | 138 def add_lowering(self, cls, f): |
139 """ Add a function to the table of lowering options for this target """ | |
140 self.lower_functions[cls] = f |