So, an update. I have managed to generate comments, although it does create a non-existent instruction. My method is as follows (and I would appreciate any comments on how to do it "better", although note that this won't make it into the final code :).)<div>
<br></div><div>1. I declared a "fake" instruction type to hold comments, ala:</div><div><br></div><div><div>class FakeInst<dag outs, dag ins, string asmstr, list<dag> pattern> : Instruction {</div><div>
  field bits<32> Inst;</div><div><br></div><div>  let Namespace = "XXX"</div><div>  let Inst{31-0} = 0;</div><div><br></div><div><div>  dag OutOperandList = outs;</div><div>  dag InOperandList = ins;</div>
<div>  let AsmString = asmstr;</div><div>  let Pattern = pattern;</div></div><div>}</div><div><br></div><div>2. I then defined a comment instruction, ala:</div><div><br></div><div><div>def COMMENT : FakeInst<(outs), (ins Reg:$fake), "; $fake", []>;</div>
</div><div><br></div><div>3. I added the following to printOperand(...) in XXXAsmPrinter.cpp:</div><div><br></div><div><div>switch (MO.getType()) {</div></div><div>  ...</div><div><div>  case MachineOperand::MO_Metadata:</div>
<div>    O << cast<MDString>(MO.getMetadata()->getOperand(0))->getString();</div><div>    break;</div></div><div>  ...</div><div>}</div><div><br></div><div>4. Finally, whenever I needed to emit comments I call the following:</div>
<div><br></div><div><div>MDNode* comment = MDNode::get(getGlobalContext(), ArrayRef<Value*>(MDString::get(getGlobalContext(), "COMMENT HERE")));</div><div>BuildMI(MBB, MBBI, dl, TII.get(XXX::COMMENT)).addMetadata(comment);</div>
</div><div><br></div><div><br></div><div><br></div><div>So, this seems to work, although it will create an empty instruction (I think). That could probably be cleaned up before MC emission, but I would really just remove the comments anyway. Any suggestions on a better way to do this (or a pointer to some obvious existing solution!) welcome :).</div>
<div><br>Stephen</div><br><div class="gmail_quote">On 20 November 2011 12:47, Stephen McGruer <span dir="ltr"><<a href="mailto:stephen.mcgruer@gmail.com">stephen.mcgruer@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Dear all,<div><br></div><div>I am looking to output assembly comments in my emitPrologue() function, just for my own readability. Searching for a way to do this found me this thread - <a href="http://lists.cs.uiuc.edu/pipermail/llvmdev/2011-October/043722.html" target="_blank">http://lists.cs.uiuc.edu/pipermail/llvmdev/2011-October/043722.html</a>, which says that the best way to output comments from somewhere like emitPrologue() is to:</div>

<div><ol><li>Create an MDString for the comment.</li><li>Attach it to an LLVM instruction using setMetadata().</li><li>Add a new member to MachineInstr to hold the metadata, and attach it when converting LLVM instructions.</li>

<li>Update AsmPrinter to read off the metadata and output it as an assembly comment.</li></ol><div>Is this the only (and correct) way to do this?</div></div><div><br></div><div><br></div><div><br></div><div><br></div><div>

If so, I am having trouble attaching the MDString to an instruction. I am using BuildMI to create my instruction, which only seems to have an "addMetadata()" function rather than "setMetadata()", and which takes an MDNode instead of an MDString. I attempted to do this as follows:</div>

<div><br></div><div>void XXXFrameLowering::emitPrologue(MachineFunction &MF) const {</div><div>...</div><div><div>  Value* end_prologue_comment = MDString::get(getGlobalContext(), "end of prologue");</div></div>

<div><div>  BuildMI(MBB, MBBI, dl, TII.get(Target::AnInstr), Target::R1).addMetadata(cast<MDNode>(end_prologue_comment));</div></div><div>}</div><div><br></div><div>However, it seems an MDString cannot be case to an MDNode this way. How do I go about turning that MDString into an MDNode, and once done how do I add the new member to MachineInstr and attach the metadata to it?</div>

<div><br></div><div>Thanks,</div><div>Stephen</div>
</blockquote></div><br></div>