When promoting an alloca to registers discard any lifetime intrinsics.
[llvm/stm8.git] / lib / MC / MCStreamer.cpp
blobae3ed0f3f61ae03dcd44223300e570cb7dee3d8d
1 //===- lib/MC/MCStreamer.cpp - Streaming Machine Code Output --------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCAsmInfo.h"
11 #include "llvm/MC/MCContext.h"
12 #include "llvm/MC/MCStreamer.h"
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/MC/MCObjectWriter.h"
15 #include "llvm/MC/MCSymbol.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "llvm/Target/TargetAsmInfo.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/Twine.h"
21 #include <cstdlib>
22 using namespace llvm;
24 MCStreamer::MCStreamer(MCContext &Ctx) : Context(Ctx), EmitEHFrame(true),
25 EmitDebugFrame(false),
26 CurrentW64UnwindInfo(0) {
27 const MCSection *section = NULL;
28 SectionStack.push_back(std::make_pair(section, section));
31 MCStreamer::~MCStreamer() {
32 for (unsigned i = 0; i < getNumW64UnwindInfos(); ++i)
33 delete W64UnwindInfos[i];
36 const MCExpr *MCStreamer::BuildSymbolDiff(MCContext &Context,
37 const MCSymbol *A,
38 const MCSymbol *B) {
39 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
40 const MCExpr *ARef =
41 MCSymbolRefExpr::Create(A, Variant, Context);
42 const MCExpr *BRef =
43 MCSymbolRefExpr::Create(B, Variant, Context);
44 const MCExpr *AddrDelta =
45 MCBinaryExpr::Create(MCBinaryExpr::Sub, ARef, BRef, Context);
46 return AddrDelta;
49 const MCExpr *MCStreamer::ForceExpAbs(const MCExpr* Expr) {
50 if (Context.getAsmInfo().hasAggressiveSymbolFolding() ||
51 isa<MCSymbolRefExpr>(Expr))
52 return Expr;
54 MCSymbol *ABS = Context.CreateTempSymbol();
55 EmitAssignment(ABS, Expr);
56 return MCSymbolRefExpr::Create(ABS, Context);
59 raw_ostream &MCStreamer::GetCommentOS() {
60 // By default, discard comments.
61 return nulls();
64 void MCStreamer::EmitDwarfSetLineAddr(int64_t LineDelta,
65 const MCSymbol *Label, int PointerSize) {
66 // emit the sequence to set the address
67 EmitIntValue(dwarf::DW_LNS_extended_op, 1);
68 EmitULEB128IntValue(PointerSize + 1);
69 EmitIntValue(dwarf::DW_LNE_set_address, 1);
70 EmitSymbolValue(Label, PointerSize);
72 // emit the sequence for the LineDelta (from 1) and a zero address delta.
73 MCDwarfLineAddr::Emit(this, LineDelta, 0);
76 /// EmitIntValue - Special case of EmitValue that avoids the client having to
77 /// pass in a MCExpr for constant integers.
78 void MCStreamer::EmitIntValue(uint64_t Value, unsigned Size,
79 unsigned AddrSpace) {
80 assert(Size <= 8 && "Invalid size");
81 assert((isUIntN(8 * Size, Value) || isIntN(8 * Size, Value)) &&
82 "Invalid size");
83 char buf[8];
84 const bool isLittleEndian = Context.getTargetAsmInfo().isLittleEndian();
85 for (unsigned i = 0; i != Size; ++i) {
86 unsigned index = isLittleEndian ? i : (Size - i - 1);
87 buf[i] = uint8_t(Value >> (index * 8));
89 EmitBytes(StringRef(buf, Size), AddrSpace);
92 /// EmitULEB128Value - Special case of EmitULEB128Value that avoids the
93 /// client having to pass in a MCExpr for constant integers.
94 void MCStreamer::EmitULEB128IntValue(uint64_t Value, unsigned AddrSpace) {
95 SmallString<32> Tmp;
96 raw_svector_ostream OSE(Tmp);
97 MCObjectWriter::EncodeULEB128(Value, OSE);
98 EmitBytes(OSE.str(), AddrSpace);
101 /// EmitSLEB128Value - Special case of EmitSLEB128Value that avoids the
102 /// client having to pass in a MCExpr for constant integers.
103 void MCStreamer::EmitSLEB128IntValue(int64_t Value, unsigned AddrSpace) {
104 SmallString<32> Tmp;
105 raw_svector_ostream OSE(Tmp);
106 MCObjectWriter::EncodeSLEB128(Value, OSE);
107 EmitBytes(OSE.str(), AddrSpace);
110 void MCStreamer::EmitAbsValue(const MCExpr *Value, unsigned Size,
111 unsigned AddrSpace) {
112 const MCExpr *ABS = ForceExpAbs(Value);
113 EmitValue(ABS, Size, AddrSpace);
117 void MCStreamer::EmitValue(const MCExpr *Value, unsigned Size,
118 unsigned AddrSpace) {
119 EmitValueImpl(Value, Size, AddrSpace);
122 void MCStreamer::EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
123 unsigned AddrSpace) {
124 EmitValueImpl(MCSymbolRefExpr::Create(Sym, getContext()), Size,
125 AddrSpace);
128 void MCStreamer::EmitGPRel32Value(const MCExpr *Value) {
129 report_fatal_error("unsupported directive in streamer");
132 /// EmitFill - Emit NumBytes bytes worth of the value specified by
133 /// FillValue. This implements directives such as '.space'.
134 void MCStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
135 unsigned AddrSpace) {
136 const MCExpr *E = MCConstantExpr::Create(FillValue, getContext());
137 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
138 EmitValue(E, 1, AddrSpace);
141 bool MCStreamer::EmitDwarfFileDirective(unsigned FileNo,
142 StringRef Filename) {
143 return getContext().GetDwarfFile(Filename, FileNo) == 0;
146 void MCStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
147 unsigned Column, unsigned Flags,
148 unsigned Isa,
149 unsigned Discriminator,
150 StringRef FileName) {
151 getContext().setCurrentDwarfLoc(FileNo, Line, Column, Flags, Isa,
152 Discriminator);
155 MCDwarfFrameInfo *MCStreamer::getCurrentFrameInfo() {
156 if (FrameInfos.empty())
157 return NULL;
158 return &FrameInfos.back();
161 void MCStreamer::EnsureValidFrame() {
162 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
163 if (!CurFrame || CurFrame->End)
164 report_fatal_error("No open frame");
167 void MCStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
168 MCSymbol *EHSymbol) {
171 void MCStreamer::EmitLabel(MCSymbol *Symbol) {
172 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
173 assert(getCurrentSection() && "Cannot emit before setting section!");
174 Symbol->setSection(*getCurrentSection());
176 StringRef Prefix = getContext().getAsmInfo().getPrivateGlobalPrefix();
177 if (!Symbol->getName().startswith(Prefix))
178 LastNonPrivate = Symbol;
181 void MCStreamer::EmitCFISections(bool EH, bool Debug) {
182 assert(EH || Debug);
183 EmitEHFrame = EH;
184 EmitDebugFrame = Debug;
187 void MCStreamer::EmitCFIStartProc() {
188 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
189 if (CurFrame && !CurFrame->End)
190 report_fatal_error("Starting a frame before finishing the previous one!");
191 MCDwarfFrameInfo Frame;
192 Frame.Begin = getContext().CreateTempSymbol();
193 Frame.Function = LastNonPrivate;
194 EmitLabel(Frame.Begin);
195 FrameInfos.push_back(Frame);
198 void MCStreamer::EmitCFIEndProc() {
199 EnsureValidFrame();
200 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
201 CurFrame->End = getContext().CreateTempSymbol();
202 EmitLabel(CurFrame->End);
205 void MCStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) {
206 EnsureValidFrame();
207 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
208 MCSymbol *Label = getContext().CreateTempSymbol();
209 EmitLabel(Label);
210 MachineLocation Dest(MachineLocation::VirtualFP);
211 MachineLocation Source(Register, -Offset);
212 MCCFIInstruction Instruction(Label, Dest, Source);
213 CurFrame->Instructions.push_back(Instruction);
216 void MCStreamer::EmitCFIDefCfaOffset(int64_t Offset) {
217 EnsureValidFrame();
218 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
219 MCSymbol *Label = getContext().CreateTempSymbol();
220 EmitLabel(Label);
221 MachineLocation Dest(MachineLocation::VirtualFP);
222 MachineLocation Source(MachineLocation::VirtualFP, -Offset);
223 MCCFIInstruction Instruction(Label, Dest, Source);
224 CurFrame->Instructions.push_back(Instruction);
227 void MCStreamer::EmitCFIAdjustCfaOffset(int64_t Adjustment) {
228 EnsureValidFrame();
229 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
230 MCSymbol *Label = getContext().CreateTempSymbol();
231 EmitLabel(Label);
232 MachineLocation Dest(MachineLocation::VirtualFP);
233 MachineLocation Source(MachineLocation::VirtualFP, Adjustment);
234 MCCFIInstruction Instruction(MCCFIInstruction::RelMove, Label, Dest, Source);
235 CurFrame->Instructions.push_back(Instruction);
238 void MCStreamer::EmitCFIDefCfaRegister(int64_t Register) {
239 EnsureValidFrame();
240 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
241 MCSymbol *Label = getContext().CreateTempSymbol();
242 EmitLabel(Label);
243 MachineLocation Dest(Register);
244 MachineLocation Source(MachineLocation::VirtualFP);
245 MCCFIInstruction Instruction(Label, Dest, Source);
246 CurFrame->Instructions.push_back(Instruction);
249 void MCStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) {
250 EnsureValidFrame();
251 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
252 MCSymbol *Label = getContext().CreateTempSymbol();
253 EmitLabel(Label);
254 MachineLocation Dest(Register, Offset);
255 MachineLocation Source(Register, Offset);
256 MCCFIInstruction Instruction(Label, Dest, Source);
257 CurFrame->Instructions.push_back(Instruction);
260 void MCStreamer::EmitCFIRelOffset(int64_t Register, int64_t Offset) {
261 EnsureValidFrame();
262 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
263 MCSymbol *Label = getContext().CreateTempSymbol();
264 EmitLabel(Label);
265 MachineLocation Dest(Register, Offset);
266 MachineLocation Source(Register, Offset);
267 MCCFIInstruction Instruction(MCCFIInstruction::RelMove, Label, Dest, Source);
268 CurFrame->Instructions.push_back(Instruction);
271 void MCStreamer::EmitCFIPersonality(const MCSymbol *Sym,
272 unsigned Encoding) {
273 EnsureValidFrame();
274 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
275 CurFrame->Personality = Sym;
276 CurFrame->PersonalityEncoding = Encoding;
279 void MCStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) {
280 EnsureValidFrame();
281 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
282 CurFrame->Lsda = Sym;
283 CurFrame->LsdaEncoding = Encoding;
286 void MCStreamer::EmitCFIRememberState() {
287 EnsureValidFrame();
288 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
289 MCSymbol *Label = getContext().CreateTempSymbol();
290 EmitLabel(Label);
291 MCCFIInstruction Instruction(MCCFIInstruction::Remember, Label);
292 CurFrame->Instructions.push_back(Instruction);
295 void MCStreamer::EmitCFIRestoreState() {
296 // FIXME: Error if there is no matching cfi_remember_state.
297 EnsureValidFrame();
298 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
299 MCSymbol *Label = getContext().CreateTempSymbol();
300 EmitLabel(Label);
301 MCCFIInstruction Instruction(MCCFIInstruction::Restore, Label);
302 CurFrame->Instructions.push_back(Instruction);
305 void MCStreamer::EmitCFISameValue(int64_t Register) {
306 EnsureValidFrame();
307 MCDwarfFrameInfo *CurFrame = getCurrentFrameInfo();
308 MCSymbol *Label = getContext().CreateTempSymbol();
309 EmitLabel(Label);
310 MCCFIInstruction Instruction(MCCFIInstruction::SameValue, Label, Register);
311 CurFrame->Instructions.push_back(Instruction);
314 void MCStreamer::setCurrentW64UnwindInfo(MCWin64EHUnwindInfo *Frame) {
315 W64UnwindInfos.push_back(Frame);
316 CurrentW64UnwindInfo = W64UnwindInfos.back();
319 void MCStreamer::EnsureValidW64UnwindInfo() {
320 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo;
321 if (!CurFrame || CurFrame->End)
322 report_fatal_error("No open Win64 EH frame function!");
325 void MCStreamer::EmitWin64EHStartProc(const MCSymbol *Symbol) {
326 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo;
327 if (CurFrame && !CurFrame->End)
328 report_fatal_error("Starting a function before ending the previous one!");
329 MCWin64EHUnwindInfo *Frame = new MCWin64EHUnwindInfo;
330 Frame->Begin = getContext().CreateTempSymbol();
331 Frame->Function = Symbol;
332 EmitLabel(Frame->Begin);
333 setCurrentW64UnwindInfo(Frame);
336 void MCStreamer::EmitWin64EHEndProc() {
337 EnsureValidW64UnwindInfo();
338 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo;
339 if (CurFrame->ChainedParent)
340 report_fatal_error("Not all chained regions terminated!");
341 CurFrame->End = getContext().CreateTempSymbol();
342 EmitLabel(CurFrame->End);
345 void MCStreamer::EmitWin64EHStartChained() {
346 EnsureValidW64UnwindInfo();
347 MCWin64EHUnwindInfo *Frame = new MCWin64EHUnwindInfo;
348 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo;
349 Frame->Begin = getContext().CreateTempSymbol();
350 Frame->Function = CurFrame->Function;
351 Frame->ChainedParent = CurFrame;
352 EmitLabel(Frame->Begin);
353 setCurrentW64UnwindInfo(Frame);
356 void MCStreamer::EmitWin64EHEndChained() {
357 EnsureValidW64UnwindInfo();
358 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo;
359 if (!CurFrame->ChainedParent)
360 report_fatal_error("End of a chained region outside a chained region!");
361 CurFrame->End = getContext().CreateTempSymbol();
362 EmitLabel(CurFrame->End);
363 CurrentW64UnwindInfo = CurFrame->ChainedParent;
366 void MCStreamer::EmitWin64EHHandler(const MCSymbol *Sym, bool Unwind,
367 bool Except) {
368 EnsureValidW64UnwindInfo();
369 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo;
370 if (CurFrame->ChainedParent)
371 report_fatal_error("Chained unwind areas can't have handlers!");
372 CurFrame->ExceptionHandler = Sym;
373 if (!Except && !Unwind)
374 report_fatal_error("Don't know what kind of handler this is!");
375 if (Unwind)
376 CurFrame->HandlesUnwind = true;
377 if (Except)
378 CurFrame->HandlesExceptions = true;
381 void MCStreamer::EmitWin64EHHandlerData() {
382 EnsureValidW64UnwindInfo();
383 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo;
384 if (CurFrame->ChainedParent)
385 report_fatal_error("Chained unwind areas can't have handlers!");
388 void MCStreamer::EmitWin64EHPushReg(unsigned Register) {
389 EnsureValidW64UnwindInfo();
390 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo;
391 MCSymbol *Label = getContext().CreateTempSymbol();
392 MCWin64EHInstruction Inst(Win64EH::UOP_PushNonVol, Label, Register);
393 EmitLabel(Label);
394 CurFrame->Instructions.push_back(Inst);
397 void MCStreamer::EmitWin64EHSetFrame(unsigned Register, unsigned Offset) {
398 EnsureValidW64UnwindInfo();
399 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo;
400 if (CurFrame->LastFrameInst >= 0)
401 report_fatal_error("Frame register and offset already specified!");
402 if (Offset & 0x0F)
403 report_fatal_error("Misaligned frame pointer offset!");
404 MCWin64EHInstruction Inst(Win64EH::UOP_SetFPReg, NULL, Register, Offset);
405 CurFrame->LastFrameInst = CurFrame->Instructions.size();
406 CurFrame->Instructions.push_back(Inst);
409 void MCStreamer::EmitWin64EHAllocStack(unsigned Size) {
410 EnsureValidW64UnwindInfo();
411 if (Size & 7)
412 report_fatal_error("Misaligned stack allocation!");
413 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo;
414 MCSymbol *Label = getContext().CreateTempSymbol();
415 MCWin64EHInstruction Inst(Label, Size);
416 EmitLabel(Label);
417 CurFrame->Instructions.push_back(Inst);
420 void MCStreamer::EmitWin64EHSaveReg(unsigned Register, unsigned Offset) {
421 EnsureValidW64UnwindInfo();
422 if (Offset & 7)
423 report_fatal_error("Misaligned saved register offset!");
424 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo;
425 MCSymbol *Label = getContext().CreateTempSymbol();
426 MCWin64EHInstruction Inst(
427 Offset > 512*1024-8 ? Win64EH::UOP_SaveNonVolBig : Win64EH::UOP_SaveNonVol,
428 Label, Register, Offset);
429 EmitLabel(Label);
430 CurFrame->Instructions.push_back(Inst);
433 void MCStreamer::EmitWin64EHSaveXMM(unsigned Register, unsigned Offset) {
434 EnsureValidW64UnwindInfo();
435 if (Offset & 0x0F)
436 report_fatal_error("Misaligned saved vector register offset!");
437 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo;
438 MCSymbol *Label = getContext().CreateTempSymbol();
439 MCWin64EHInstruction Inst(
440 Offset > 512*1024-16 ? Win64EH::UOP_SaveXMM128Big : Win64EH::UOP_SaveXMM128,
441 Label, Register, Offset);
442 EmitLabel(Label);
443 CurFrame->Instructions.push_back(Inst);
446 void MCStreamer::EmitWin64EHPushFrame(bool Code) {
447 EnsureValidW64UnwindInfo();
448 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo;
449 if (CurFrame->Instructions.size() > 0)
450 report_fatal_error("If present, PushMachFrame must be the first UOP");
451 MCSymbol *Label = getContext().CreateTempSymbol();
452 MCWin64EHInstruction Inst(Win64EH::UOP_PushMachFrame, Label, Code);
453 EmitLabel(Label);
454 CurFrame->Instructions.push_back(Inst);
457 void MCStreamer::EmitWin64EHEndProlog() {
458 EnsureValidW64UnwindInfo();
459 MCWin64EHUnwindInfo *CurFrame = CurrentW64UnwindInfo;
460 CurFrame->PrologEnd = getContext().CreateTempSymbol();
461 EmitLabel(CurFrame->PrologEnd);
464 void MCStreamer::EmitFnStart() {
465 errs() << "Not implemented yet\n";
466 abort();
469 void MCStreamer::EmitFnEnd() {
470 errs() << "Not implemented yet\n";
471 abort();
474 void MCStreamer::EmitCantUnwind() {
475 errs() << "Not implemented yet\n";
476 abort();
479 void MCStreamer::EmitHandlerData() {
480 errs() << "Not implemented yet\n";
481 abort();
484 void MCStreamer::EmitPersonality(const MCSymbol *Personality) {
485 errs() << "Not implemented yet\n";
486 abort();
489 void MCStreamer::EmitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset) {
490 errs() << "Not implemented yet\n";
491 abort();
494 void MCStreamer::EmitPad(int64_t Offset) {
495 errs() << "Not implemented yet\n";
496 abort();
499 void MCStreamer::EmitRegSave(const SmallVectorImpl<unsigned> &RegList, bool) {
500 errs() << "Not implemented yet\n";
501 abort();
504 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
505 /// the specified string in the output .s file. This capability is
506 /// indicated by the hasRawTextSupport() predicate.
507 void MCStreamer::EmitRawText(StringRef String) {
508 errs() << "EmitRawText called on an MCStreamer that doesn't support it, "
509 " something must not be fully mc'ized\n";
510 abort();
513 void MCStreamer::EmitRawText(const Twine &T) {
514 SmallString<128> Str;
515 T.toVector(Str);
516 EmitRawText(Str.str());
519 void MCStreamer::EmitFrames(bool usingCFI) {
520 if (!getNumFrameInfos())
521 return;
523 if (EmitEHFrame)
524 MCDwarfFrameEmitter::Emit(*this, usingCFI, true);
526 if (EmitDebugFrame)
527 MCDwarfFrameEmitter::Emit(*this, usingCFI, false);
530 void MCStreamer::EmitW64Tables() {
531 if (!getNumW64UnwindInfos())
532 return;
534 MCWin64EHUnwindEmitter::Emit(*this);