|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | import re |
6 | | -from . import opcodes |
7 | 6 | from .nocomment import remove_comments as do_remove_comments |
8 | 7 | from .util import garbage_collect |
9 | 8 |
|
@@ -88,9 +87,19 @@ def set_global(self, symbol): |
88 | 87 |
|
89 | 88 | class Assembler: |
90 | 89 |
|
91 | | - def __init__(self, symbols=None, bases=None, globals=None): |
| 90 | + def __init__(self, cpu='esp32', symbols=None, bases=None, globals=None): |
| 91 | + if cpu == 'esp32': |
| 92 | + opcode_module = 'opcodes' |
| 93 | + elif cpu == 'esp32s2': |
| 94 | + opcode_module = 'opcodes_s2' |
| 95 | + else: |
| 96 | + raise ValueError('Invalid cpu') |
| 97 | + |
| 98 | + relative_import = 1 if '/' in __file__ else 0 |
| 99 | + self.opcodes = __import__(opcode_module, None, None, [], relative_import) |
| 100 | + |
92 | 101 | self.symbols = SymbolTable(symbols or {}, bases or {}, globals or {}) |
93 | | - opcodes.symbols = self.symbols # XXX dirty hack |
| 102 | + self.opcodes.symbols = self.symbols # XXX dirty hack |
94 | 103 |
|
95 | 104 | # regex for parsing assembly lines |
96 | 105 | # format: [[whitespace]label:][whitespace][opcode[whitespace arg[,arg...]]] |
@@ -223,7 +232,7 @@ def d_align(self, align=4, fill=None): |
223 | 232 | self.fill(self.section, amount, fill) |
224 | 233 |
|
225 | 234 | def d_set(self, symbol, expr): |
226 | | - value = int(opcodes.eval_arg(expr)) |
| 235 | + value = int(self.opcodes.eval_arg(expr)) |
227 | 236 | self.symbols.set_sym(symbol, ABS, None, value) |
228 | 237 |
|
229 | 238 | def d_global(self, symbol): |
@@ -264,13 +273,13 @@ def assembler_pass(self, lines): |
264 | 273 | else: |
265 | 274 | # machine instruction |
266 | 275 | opcode_lower = opcode.lower() |
267 | | - func = getattr(opcodes, 'i_' + opcode_lower, None) |
| 276 | + func = getattr(self.opcodes, 'i_' + opcode_lower, None) |
268 | 277 | if func is not None: |
269 | 278 | if self.a_pass == 1: |
270 | 279 | # during the first pass, symbols are not all known yet. |
271 | 280 | # so we add empty instructions to the section, to determine |
272 | 281 | # section sizes and symbol offsets for pass 2. |
273 | | - result = (0,) * opcodes.no_of_instr(opcode_lower, args) |
| 282 | + result = (0,) * self.opcodes.no_of_instr(opcode_lower, args) |
274 | 283 | else: |
275 | 284 | result = func(*args) |
276 | 285 |
|
|
0 commit comments