106 def parse_txt(mnemo, attrib, txt, symbol_pool=None):
107 """Parse an assembly listing. Returns a couple (blocks, symbol_pool), where
108 blocks is a list of asm_bloc and symbol_pool the associated asm_symbol_pool
110 @mnemo: architecture used
111 @attrib: architecture attribute
112 @txt: assembly listing
113 @symbol_pool: (optional) the asm_symbol_pool instance used to handle labels
118 if symbol_pool
is None:
121 C_NEXT = asmbloc.asm_constraint.c_next
122 C_TO = asmbloc.asm_constraint.c_to
126 for line
in txt.split(
'\n'):
128 if EMPTY_RE.match(line):
131 if COMMENT_RE.match(line):
134 if FORGET_LABEL_RE.match(line):
137 match_re = LABEL_RE.match(line)
139 label_name = match_re.group(1)
140 label = symbol_pool.getby_name_create(label_name)
144 if DIRECTIVE_START_RE.match(line):
145 match_re = DIRECTIVE_RE.match(line)
146 directive = match_re.group(1)
147 if directive
in [
'text',
'data',
'bss']:
149 if directive
in [
'string',
'ascii']:
151 line = line.replace(
r'\n',
'\n').replace(
r'\r',
'\r')
152 raw = line[line.find(
r'"') + 1:line.rfind(
r'"')]
153 raw = raw.decode(
'string_escape')
154 if directive ==
'string':
158 if directive ==
'ustring':
160 line = line.replace(
r'\n',
'\n').replace(
r'\r',
'\r')
161 raw = line[line.find(
r'"') + 1:line.rfind(
r'"')] +
"\x00"
162 raw = raw.decode(
'string_escape')
163 raw =
"".join([string +
'\x00' for string
in raw])
166 if directive
in declarator:
167 data_raw = line[match_re.end():].split(
' ', 1)[1]
168 data_raw = data_raw.split(
',')
169 size = declarator[directive]
174 my_var_parser =
parse_ast(
lambda x: m2_expr.ExprId(x, size),
176 m2_expr.ExprInt(x, size))
177 base_expr.setParseAction(my_var_parser)
179 for element
in data_raw:
180 element = element.strip()
181 element_expr = base_expr.parseString(element)[0]
182 expr_list.append(element_expr.canonize())
185 raw_data.element_size = size
186 lines.append(raw_data)
188 if directive ==
'comm':
191 if directive ==
'split':
194 if directive ==
'dontsplit':
197 if directive ==
"align":
198 align_value = int(line[match_re.end():], 0)
201 if directive
in [
'file',
'intel_syntax',
'globl',
'local',
202 'type',
'size',
'align',
'ident',
'section']:
204 if directive[0:4] ==
'cfi_':
207 raise ValueError(
"unknown directive %s" % str(directive))
210 match_re = LABEL_RE.match(line)
212 label_name = match_re.group(1)
213 label = symbol_pool.getby_name_create(label_name)
219 line = line[:line.find(
';')]
220 line = line.strip(
' ').strip(
'\t')
221 instr = mnemo.fromstring(line, attrib)
227 instr.dstflow2label(symbol_pool)
230 asmbloc.log_asmbloc.info(
"___pre asm oki___")
234 state = STATE_NO_BLOC
237 block_to_nlink =
None
238 block_may_link =
False
240 while i < len(lines):
243 state = STATE_NO_BLOC
248 if state == STATE_NO_BLOC:
249 if isinstance(line, DirectiveDontSplit):
250 block_to_nlink = cur_block
253 elif isinstance(line, DirectiveSplit):
254 block_to_nlink =
None
266 blocks.append(cur_block)
267 state = STATE_IN_BLOC
269 block_to_nlink.addto(
272 block_to_nlink =
None
276 elif state == STATE_IN_BLOC:
277 if isinstance(line, DirectiveSplit):
278 state = STATE_NO_BLOC
279 block_to_nlink =
None
280 elif isinstance(line, DirectiveDontSplit):
281 state = STATE_NO_BLOC
282 block_to_nlink = cur_block
283 elif isinstance(line, DirectiveAlign):
284 cur_block.alignment = line.alignment
286 cur_block.addline(line)
287 block_to_nlink = cur_block
292 block_to_nlink =
None
293 state = STATE_NO_BLOC
296 elif isinstance(line, instruction):
297 cur_block.addline(line)
298 block_to_nlink = cur_block
299 if not line.breakflow():
303 raise RuntimeError(
"Cannot have breakflow in delayslot")
305 for dst
in line.getdstflow(symbol_pool):
306 if not isinstance(dst, m2_expr.ExprId):
308 if dst
in mnemo.regs.all_regs_ids:
312 if not line.splitflow():
313 block_to_nlink =
None
315 delayslot = line.delayslot
317 state = STATE_NO_BLOC
319 raise RuntimeError(
"unknown class %s" % line.__class__)
323 asmbloc.log_asmbloc.info(block)
324 return blocks, symbol_pool
def replace_orphan_labels