<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - Assembly Parser Emits .thumb_func and Conditional Instructions Too Late After Function Label"
href="https://bugs.llvm.org/show_bug.cgi?id=34002">34002</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Assembly Parser Emits .thumb_func and Conditional Instructions Too Late After Function Label
</td>
</tr>
<tr>
<th>Product</th>
<td>libraries
</td>
</tr>
<tr>
<th>Version</th>
<td>4.0
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>normal
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>Backend: ARM
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>editing@zju.edu.cn
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>Created <span class=""><a href="attachment.cgi?id=18877" name="attach_18877" title="Original assembly code it-block.S and generated assembly code it-block-roundtrip.s">attachment 18877</a> <a href="attachment.cgi?id=18877&action=edit" title="Original assembly code it-block.S and generated assembly code it-block-roundtrip.s">[details]</a></span>
Original assembly code it-block.S and generated assembly code
it-block-roundtrip.s
Overview: When parsing a function label, AsmParser emits the .thumb_func
directive and closes the open IT block (if one is open right before the
function label) too late, so that both the .thumb_func directive and
conditional instructions of the IT block are emitted after the function label.
Steps to Reproduce:
1) Save the following native assembly code to it-block.S:
.text
.syntax unified
.p2align 1
.code 16
.globl f1
.globl f2
.thumb_func
f1:
CMP r0, #10
.thumb_func
MOVPL r0, #0
f2:
MOVS r1, #0
.Ltmp:
CMP r0, #0
ITTT PL
ADDPL r1, r1, r0
SUBPL r0, r0, #1
BPL .Ltmp
MOV r0, r1
BX lr
.end
2) Run llvm-mc on it-block.S and ask it to generate native assembly code, using
the following command line:
llvm-mc -arch=thumb -filetype=asm
-mattr=+soft-float,-neon,-crypto,+strict-align -mcpu=cortex-m3 -n
-triple=armv7m-none-none-eabi -o=it-block-roundtrip.s it-block.S
-arm-implicit-it=always
Actual Results: The native assembly code generated by llvm-mc is as follows:
.text
.p2align 1
.code 16
.globl f1
.globl f2
f1:
.thumb_func
cmp r0, #10
f2:
it pl
movpl r0, #0
.thumb_func
movs r1, #0
.Ltmp:
cmp r0, #0
ittt pl
addpl r1, r1, r0
subpl r0, r0, #1
bpl .Ltmp
mov r0, r1
bx lr
Note two types of errors: i. For both function labels f1 and f2, the
corresponding .thumb_func directive is after the function label; ii. The movpl
instruction, which should be before f2 and is NOT part of function f2, is
emitted after f2 and becomes part of function f2.
Expected Results: i. For both function labels f1 and f2, the corresponding
.thumb_func directive should be before the label; ii. The movepl instruction
should be before label f2.
Additional information:
As the following code snippet copied from lib/MC/MCParser/AsmParser.cpp shows,
when parsing a label, AsmParser::parseStatement calls
MCTargetAsmParser::onLabelParsed of a target parser after emitting the label:
// Emit the label.
if (!getTargetParser().isParsingInlineAsm())
Out.EmitLabel(Sym, IDLoc);
// If we are generating dwarf for assembly source files then gather the
// info to make a dwarf label entry for this label if needed.
if (getContext().getGenDwarfForAssembly())
MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(),
IDLoc);
getTargetParser().onLabelParsed(Sym);
For ARM, calling MCTargetAsmParser::onLabelParsed after emitting the label
seems to be the cause of the bug.
If I understand it correctly, ARMAsmParser::onLabelParsed (defined in
lib/Target/ARM/AsmParser/ARMAsmParser.cpp) performs two tasks: i. Close the
current implicit IT block (if one is open for new conditional instructions)
BEFORE the label, so that the IT block cannot be entered from the middle of it.
ii. Emit a .thumb_func directive, if the label is the first label following a
previously parsed .thumb_func directive without an optional symbol. It seems
that the two types of errors are direct consequences of calling
MCTargetAsmParser::onLabelParsed after emitting the label being parsed.
What's more, MCTargetAsmParser::onLabelParsed seems to be only used by the ARM
backend.
Therefore, in order to fix the bug, it seems sufficient to call
MCTargetAsmParser::onLabelParsed before emitting the label being parsed.</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>