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