LLVM 23.0.0git
SystemZAsmPrinter.cpp
Go to the documentation of this file.
1//===-- SystemZAsmPrinter.cpp - SystemZ LLVM assembly printer -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Streams SystemZ assembly language and associated data, in the form of
10// MCInsts and MCExprs respectively.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SystemZAsmPrinter.h"
20#include "SystemZMCInstLower.h"
27#include "llvm/IR/Mangler.h"
28#include "llvm/IR/Module.h"
30#include "llvm/MC/MCExpr.h"
33#include "llvm/MC/MCStreamer.h"
36#include "llvm/Support/Chrono.h"
40
41using namespace llvm;
42
43// Return an RI instruction like MI with opcode Opcode, but with the
44// GR64 register operands turned into GR32s.
45static MCInst lowerRILow(const MachineInstr *MI, unsigned Opcode) {
46 if (MI->isCompare())
47 return MCInstBuilder(Opcode)
48 .addReg(SystemZMC::getRegAsGR32(MI->getOperand(0).getReg()))
49 .addImm(MI->getOperand(1).getImm());
50 else
51 return MCInstBuilder(Opcode)
52 .addReg(SystemZMC::getRegAsGR32(MI->getOperand(0).getReg()))
53 .addReg(SystemZMC::getRegAsGR32(MI->getOperand(1).getReg()))
54 .addImm(MI->getOperand(2).getImm());
55}
56
57// Return an RI instruction like MI with opcode Opcode, but with the
58// GR64 register operands turned into GRH32s.
59static MCInst lowerRIHigh(const MachineInstr *MI, unsigned Opcode) {
60 if (MI->isCompare())
61 return MCInstBuilder(Opcode)
62 .addReg(SystemZMC::getRegAsGRH32(MI->getOperand(0).getReg()))
63 .addImm(MI->getOperand(1).getImm());
64 else
65 return MCInstBuilder(Opcode)
66 .addReg(SystemZMC::getRegAsGRH32(MI->getOperand(0).getReg()))
67 .addReg(SystemZMC::getRegAsGRH32(MI->getOperand(1).getReg()))
68 .addImm(MI->getOperand(2).getImm());
69}
70
71// Return an RI instruction like MI with opcode Opcode, but with the
72// R2 register turned into a GR64.
73static MCInst lowerRIEfLow(const MachineInstr *MI, unsigned Opcode) {
74 return MCInstBuilder(Opcode)
75 .addReg(MI->getOperand(0).getReg())
76 .addReg(MI->getOperand(1).getReg())
77 .addReg(SystemZMC::getRegAsGR64(MI->getOperand(2).getReg()))
78 .addImm(MI->getOperand(3).getImm())
79 .addImm(MI->getOperand(4).getImm())
80 .addImm(MI->getOperand(5).getImm());
81}
82
83static const MCSymbolRefExpr *getTLSGetOffset(MCContext &Context) {
84 StringRef Name = "__tls_get_offset";
85 return MCSymbolRefExpr::create(Context.getOrCreateSymbol(Name),
86 SystemZ::S_PLT, Context);
87}
88
90 StringRef Name = "_GLOBAL_OFFSET_TABLE_";
91 return MCSymbolRefExpr::create(Context.getOrCreateSymbol(Name),
92 Context);
93}
94
95// MI is an instruction that accepts an optional alignment hint,
96// and which was already lowered to LoweredMI. If the alignment
97// of the original memory operand is known, update LoweredMI to
98// an instruction with the corresponding hint set.
99static void lowerAlignmentHint(const MachineInstr *MI, MCInst &LoweredMI,
100 unsigned Opcode) {
101 if (MI->memoperands_empty())
102 return;
103
104 Align Alignment = Align(16);
105 for (MachineInstr::mmo_iterator MMOI = MI->memoperands_begin(),
106 EE = MI->memoperands_end(); MMOI != EE; ++MMOI)
107 if ((*MMOI)->getAlign() < Alignment)
108 Alignment = (*MMOI)->getAlign();
109
110 unsigned AlignmentHint = 0;
111 if (Alignment >= Align(16))
112 AlignmentHint = 4;
113 else if (Alignment >= Align(8))
114 AlignmentHint = 3;
115 if (AlignmentHint == 0)
116 return;
117
118 LoweredMI.setOpcode(Opcode);
119 LoweredMI.addOperand(MCOperand::createImm(AlignmentHint));
120}
121
122// MI loads the high part of a vector from memory. Return an instruction
123// that uses replicating vector load Opcode to do the same thing.
124static MCInst lowerSubvectorLoad(const MachineInstr *MI, unsigned Opcode) {
125 return MCInstBuilder(Opcode)
126 .addReg(SystemZMC::getRegAsVR128(MI->getOperand(0).getReg()))
127 .addReg(MI->getOperand(1).getReg())
128 .addImm(MI->getOperand(2).getImm())
129 .addReg(MI->getOperand(3).getReg());
130}
131
132// MI stores the high part of a vector to memory. Return an instruction
133// that uses elemental vector store Opcode to do the same thing.
134static MCInst lowerSubvectorStore(const MachineInstr *MI, unsigned Opcode) {
135 return MCInstBuilder(Opcode)
136 .addReg(SystemZMC::getRegAsVR128(MI->getOperand(0).getReg()))
137 .addReg(MI->getOperand(1).getReg())
138 .addImm(MI->getOperand(2).getImm())
139 .addReg(MI->getOperand(3).getReg())
140 .addImm(0);
141}
142
143// MI extracts the first element of the source vector.
144static MCInst lowerVecEltExtraction(const MachineInstr *MI, unsigned Opcode) {
145 return MCInstBuilder(Opcode)
146 .addReg(SystemZMC::getRegAsGR64(MI->getOperand(0).getReg()))
147 .addReg(SystemZMC::getRegAsVR128(MI->getOperand(1).getReg()))
148 .addReg(0)
149 .addImm(0);
150}
151
152// MI inserts value into the first element of the destination vector.
153static MCInst lowerVecEltInsertion(const MachineInstr *MI, unsigned Opcode) {
154 return MCInstBuilder(Opcode)
155 .addReg(SystemZMC::getRegAsVR128(MI->getOperand(0).getReg()))
156 .addReg(SystemZMC::getRegAsVR128(MI->getOperand(0).getReg()))
157 .addReg(MI->getOperand(1).getReg())
158 .addReg(0)
159 .addImm(0);
160}
161
162// The XPLINK ABI requires that a no-op encoding the call type is emitted after
163// each call to a subroutine. This information can be used by the called
164// function to determine its entry point, e.g. for generating a backtrace. The
165// call type is encoded as a register number in the bcr instruction. See
166// enumeration CallType for the possible values.
167void SystemZAsmPrinter::emitCallInformation(CallType CT) {
169 MCInstBuilder(SystemZ::BCRAsm)
170 .addImm(0)
171 .addReg(SystemZMC::GR64Regs[static_cast<unsigned>(CT)]));
172}
173
174uint32_t SystemZAsmPrinter::AssociatedDataAreaTable::insert(const MCSymbol *Sym,
175 unsigned SlotKind) {
176 auto Key = std::make_pair(Sym, SlotKind);
177 auto It = Displacements.find(Key);
178
179 if (It != Displacements.end())
180 return (*It).second;
181
182 // Determine length of descriptor.
183 uint32_t Length;
184 switch (SlotKind) {
186 Length = 2 * PointerSize;
187 break;
188 default:
189 Length = PointerSize;
190 break;
191 }
192
193 uint32_t Displacement = NextDisplacement;
194 Displacements[std::make_pair(Sym, SlotKind)] = NextDisplacement;
195 NextDisplacement += Length;
196
197 return Displacement;
198}
199
200uint32_t
201SystemZAsmPrinter::AssociatedDataAreaTable::insert(const MachineOperand MO) {
202 MCSymbol *Sym;
204 const GlobalValue *GV = MO.getGlobal();
205 Sym = MO.getParent()->getMF()->getTarget().getSymbol(GV);
206 assert(Sym && "No symbol");
207 } else if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
208 const char *SymName = MO.getSymbolName();
209 Sym = MO.getParent()->getMF()->getContext().getOrCreateSymbol(SymName);
210 assert(Sym && "No symbol");
211 } else
212 llvm_unreachable("Unexpected operand type");
213
214 unsigned ADAslotType = MO.getTargetFlags();
215 return insert(Sym, ADAslotType);
216}
217
219 SystemZ_MC::verifyInstructionPredicates(MI->getOpcode(),
220 getSubtargetInfo().getFeatureBits());
221
222 SystemZMCInstLower Lower(MF->getContext(), *this);
223 MCInst LoweredMI;
224 switch (MI->getOpcode()) {
225 case SystemZ::Return:
226 LoweredMI = MCInstBuilder(SystemZ::BR)
227 .addReg(SystemZ::R14D);
228 break;
229
230 case SystemZ::Return_XPLINK:
231 LoweredMI = MCInstBuilder(SystemZ::B)
232 .addReg(SystemZ::R7D)
233 .addImm(2)
234 .addReg(0);
235 break;
236
237 case SystemZ::CondReturn:
238 LoweredMI = MCInstBuilder(SystemZ::BCR)
239 .addImm(MI->getOperand(0).getImm())
240 .addImm(MI->getOperand(1).getImm())
241 .addReg(SystemZ::R14D);
242 break;
243
244 case SystemZ::CondReturn_XPLINK:
245 LoweredMI = MCInstBuilder(SystemZ::BC)
246 .addImm(MI->getOperand(0).getImm())
247 .addImm(MI->getOperand(1).getImm())
248 .addReg(SystemZ::R7D)
249 .addImm(2)
250 .addReg(0);
251 break;
252
253 case SystemZ::CRBReturn:
254 LoweredMI = MCInstBuilder(SystemZ::CRB)
255 .addReg(MI->getOperand(0).getReg())
256 .addReg(MI->getOperand(1).getReg())
257 .addImm(MI->getOperand(2).getImm())
258 .addReg(SystemZ::R14D)
259 .addImm(0);
260 break;
261
262 case SystemZ::CGRBReturn:
263 LoweredMI = MCInstBuilder(SystemZ::CGRB)
264 .addReg(MI->getOperand(0).getReg())
265 .addReg(MI->getOperand(1).getReg())
266 .addImm(MI->getOperand(2).getImm())
267 .addReg(SystemZ::R14D)
268 .addImm(0);
269 break;
270
271 case SystemZ::CIBReturn:
272 LoweredMI = MCInstBuilder(SystemZ::CIB)
273 .addReg(MI->getOperand(0).getReg())
274 .addImm(MI->getOperand(1).getImm())
275 .addImm(MI->getOperand(2).getImm())
276 .addReg(SystemZ::R14D)
277 .addImm(0);
278 break;
279
280 case SystemZ::CGIBReturn:
281 LoweredMI = MCInstBuilder(SystemZ::CGIB)
282 .addReg(MI->getOperand(0).getReg())
283 .addImm(MI->getOperand(1).getImm())
284 .addImm(MI->getOperand(2).getImm())
285 .addReg(SystemZ::R14D)
286 .addImm(0);
287 break;
288
289 case SystemZ::CLRBReturn:
290 LoweredMI = MCInstBuilder(SystemZ::CLRB)
291 .addReg(MI->getOperand(0).getReg())
292 .addReg(MI->getOperand(1).getReg())
293 .addImm(MI->getOperand(2).getImm())
294 .addReg(SystemZ::R14D)
295 .addImm(0);
296 break;
297
298 case SystemZ::CLGRBReturn:
299 LoweredMI = MCInstBuilder(SystemZ::CLGRB)
300 .addReg(MI->getOperand(0).getReg())
301 .addReg(MI->getOperand(1).getReg())
302 .addImm(MI->getOperand(2).getImm())
303 .addReg(SystemZ::R14D)
304 .addImm(0);
305 break;
306
307 case SystemZ::CLIBReturn:
308 LoweredMI = MCInstBuilder(SystemZ::CLIB)
309 .addReg(MI->getOperand(0).getReg())
310 .addImm(MI->getOperand(1).getImm())
311 .addImm(MI->getOperand(2).getImm())
312 .addReg(SystemZ::R14D)
313 .addImm(0);
314 break;
315
316 case SystemZ::CLGIBReturn:
317 LoweredMI = MCInstBuilder(SystemZ::CLGIB)
318 .addReg(MI->getOperand(0).getReg())
319 .addImm(MI->getOperand(1).getImm())
320 .addImm(MI->getOperand(2).getImm())
321 .addReg(SystemZ::R14D)
322 .addImm(0);
323 break;
324
325 case SystemZ::CallBRASL_XPLINK64:
327 .addReg(SystemZ::R7D)
328 .addExpr(Lower.getExpr(MI->getOperand(0),
330 emitCallInformation(CallType::BRASL7);
331 return;
332
333 case SystemZ::CallBASR_XPLINK64:
335 .addReg(SystemZ::R7D)
336 .addReg(MI->getOperand(0).getReg()));
337 emitCallInformation(CallType::BASR76);
338 return;
339
340 case SystemZ::CallBASR_STACKEXT:
342 .addReg(SystemZ::R3D)
343 .addReg(MI->getOperand(0).getReg()));
344 emitCallInformation(CallType::BASR33);
345 return;
346
347 case SystemZ::ADA_ENTRY_VALUE:
348 case SystemZ::ADA_ENTRY: {
349 const SystemZSubtarget &Subtarget = MF->getSubtarget<SystemZSubtarget>();
350 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
351 uint32_t Disp = ADATable.insert(MI->getOperand(1));
352 Register TargetReg = MI->getOperand(0).getReg();
353
354 Register ADAReg = MI->getOperand(2).getReg();
355 Disp += MI->getOperand(3).getImm();
356 bool LoadAddr = MI->getOpcode() == SystemZ::ADA_ENTRY;
357
358 unsigned Op0 = LoadAddr ? SystemZ::LA : SystemZ::LG;
359 unsigned Op = TII->getOpcodeForOffset(Op0, Disp);
360
361 Register IndexReg = 0;
362 if (!Op) {
363 if (TargetReg != ADAReg) {
364 IndexReg = TargetReg;
365 // Use TargetReg to store displacement.
368 MCInstBuilder(SystemZ::LLILF).addReg(TargetReg).addImm(Disp));
369 } else
371 .addReg(TargetReg)
372 .addReg(TargetReg)
373 .addImm(Disp));
374 Disp = 0;
375 Op = Op0;
376 }
378 .addReg(TargetReg)
379 .addReg(ADAReg)
380 .addImm(Disp)
381 .addReg(IndexReg));
382
383 return;
384 }
385 case SystemZ::CallBRASL:
386 LoweredMI = MCInstBuilder(SystemZ::BRASL)
387 .addReg(SystemZ::R14D)
388 .addExpr(Lower.getExpr(MI->getOperand(0), SystemZ::S_PLT));
389 break;
390
391 case SystemZ::CallBASR:
392 LoweredMI = MCInstBuilder(SystemZ::BASR)
393 .addReg(SystemZ::R14D)
394 .addReg(MI->getOperand(0).getReg());
395 break;
396
397 case SystemZ::CallJG:
398 LoweredMI = MCInstBuilder(SystemZ::JG)
399 .addExpr(Lower.getExpr(MI->getOperand(0), SystemZ::S_PLT));
400 break;
401
402 case SystemZ::CallBRCL:
403 LoweredMI = MCInstBuilder(SystemZ::BRCL)
404 .addImm(MI->getOperand(0).getImm())
405 .addImm(MI->getOperand(1).getImm())
406 .addExpr(Lower.getExpr(MI->getOperand(2), SystemZ::S_PLT));
407 break;
408
409 case SystemZ::CallBR:
410 LoweredMI = MCInstBuilder(SystemZ::BR)
411 .addReg(MI->getOperand(0).getReg());
412 break;
413
414 case SystemZ::CallBCR:
415 LoweredMI = MCInstBuilder(SystemZ::BCR)
416 .addImm(MI->getOperand(0).getImm())
417 .addImm(MI->getOperand(1).getImm())
418 .addReg(MI->getOperand(2).getReg());
419 break;
420
421 case SystemZ::CRBCall:
422 LoweredMI = MCInstBuilder(SystemZ::CRB)
423 .addReg(MI->getOperand(0).getReg())
424 .addReg(MI->getOperand(1).getReg())
425 .addImm(MI->getOperand(2).getImm())
426 .addReg(MI->getOperand(3).getReg())
427 .addImm(0);
428 break;
429
430 case SystemZ::CGRBCall:
431 LoweredMI = MCInstBuilder(SystemZ::CGRB)
432 .addReg(MI->getOperand(0).getReg())
433 .addReg(MI->getOperand(1).getReg())
434 .addImm(MI->getOperand(2).getImm())
435 .addReg(MI->getOperand(3).getReg())
436 .addImm(0);
437 break;
438
439 case SystemZ::CIBCall:
440 LoweredMI = MCInstBuilder(SystemZ::CIB)
441 .addReg(MI->getOperand(0).getReg())
442 .addImm(MI->getOperand(1).getImm())
443 .addImm(MI->getOperand(2).getImm())
444 .addReg(MI->getOperand(3).getReg())
445 .addImm(0);
446 break;
447
448 case SystemZ::CGIBCall:
449 LoweredMI = MCInstBuilder(SystemZ::CGIB)
450 .addReg(MI->getOperand(0).getReg())
451 .addImm(MI->getOperand(1).getImm())
452 .addImm(MI->getOperand(2).getImm())
453 .addReg(MI->getOperand(3).getReg())
454 .addImm(0);
455 break;
456
457 case SystemZ::CLRBCall:
458 LoweredMI = MCInstBuilder(SystemZ::CLRB)
459 .addReg(MI->getOperand(0).getReg())
460 .addReg(MI->getOperand(1).getReg())
461 .addImm(MI->getOperand(2).getImm())
462 .addReg(MI->getOperand(3).getReg())
463 .addImm(0);
464 break;
465
466 case SystemZ::CLGRBCall:
467 LoweredMI = MCInstBuilder(SystemZ::CLGRB)
468 .addReg(MI->getOperand(0).getReg())
469 .addReg(MI->getOperand(1).getReg())
470 .addImm(MI->getOperand(2).getImm())
471 .addReg(MI->getOperand(3).getReg())
472 .addImm(0);
473 break;
474
475 case SystemZ::CLIBCall:
476 LoweredMI = MCInstBuilder(SystemZ::CLIB)
477 .addReg(MI->getOperand(0).getReg())
478 .addImm(MI->getOperand(1).getImm())
479 .addImm(MI->getOperand(2).getImm())
480 .addReg(MI->getOperand(3).getReg())
481 .addImm(0);
482 break;
483
484 case SystemZ::CLGIBCall:
485 LoweredMI = MCInstBuilder(SystemZ::CLGIB)
486 .addReg(MI->getOperand(0).getReg())
487 .addImm(MI->getOperand(1).getImm())
488 .addImm(MI->getOperand(2).getImm())
489 .addReg(MI->getOperand(3).getReg())
490 .addImm(0);
491 break;
492
493 case SystemZ::TLS_GDCALL:
494 LoweredMI =
495 MCInstBuilder(SystemZ::BRASL)
496 .addReg(SystemZ::R14D)
497 .addExpr(getTLSGetOffset(MF->getContext()))
498 .addExpr(Lower.getExpr(MI->getOperand(0), SystemZ::S_TLSGD));
499 break;
500
501 case SystemZ::TLS_LDCALL:
502 LoweredMI =
503 MCInstBuilder(SystemZ::BRASL)
504 .addReg(SystemZ::R14D)
505 .addExpr(getTLSGetOffset(MF->getContext()))
506 .addExpr(Lower.getExpr(MI->getOperand(0), SystemZ::S_TLSLDM));
507 break;
508
509 case SystemZ::GOT:
510 LoweredMI = MCInstBuilder(SystemZ::LARL)
511 .addReg(MI->getOperand(0).getReg())
512 .addExpr(getGlobalOffsetTable(MF->getContext()));
513 break;
514
515 case SystemZ::IILF64:
516 LoweredMI = MCInstBuilder(SystemZ::IILF)
517 .addReg(SystemZMC::getRegAsGR32(MI->getOperand(0).getReg()))
518 .addImm(MI->getOperand(2).getImm());
519 break;
520
521 case SystemZ::IIHF64:
522 LoweredMI = MCInstBuilder(SystemZ::IIHF)
523 .addReg(SystemZMC::getRegAsGRH32(MI->getOperand(0).getReg()))
524 .addImm(MI->getOperand(2).getImm());
525 break;
526
527 case SystemZ::RISBHH:
528 case SystemZ::RISBHL:
529 LoweredMI = lowerRIEfLow(MI, SystemZ::RISBHG);
530 break;
531
532 case SystemZ::RISBLH:
533 case SystemZ::RISBLL:
534 LoweredMI = lowerRIEfLow(MI, SystemZ::RISBLG);
535 break;
536
537 case SystemZ::VLVGP32:
538 LoweredMI = MCInstBuilder(SystemZ::VLVGP)
539 .addReg(MI->getOperand(0).getReg())
540 .addReg(SystemZMC::getRegAsGR64(MI->getOperand(1).getReg()))
541 .addReg(SystemZMC::getRegAsGR64(MI->getOperand(2).getReg()));
542 break;
543
544 case SystemZ::VLR32:
545 case SystemZ::VLR64:
546 LoweredMI = MCInstBuilder(SystemZ::VLR)
547 .addReg(SystemZMC::getRegAsVR128(MI->getOperand(0).getReg()))
548 .addReg(SystemZMC::getRegAsVR128(MI->getOperand(1).getReg()));
549 break;
550
551 case SystemZ::VL:
552 Lower.lower(MI, LoweredMI);
553 lowerAlignmentHint(MI, LoweredMI, SystemZ::VLAlign);
554 break;
555
556 case SystemZ::VST:
557 Lower.lower(MI, LoweredMI);
558 lowerAlignmentHint(MI, LoweredMI, SystemZ::VSTAlign);
559 break;
560
561 case SystemZ::VLM:
562 Lower.lower(MI, LoweredMI);
563 lowerAlignmentHint(MI, LoweredMI, SystemZ::VLMAlign);
564 break;
565
566 case SystemZ::VSTM:
567 Lower.lower(MI, LoweredMI);
568 lowerAlignmentHint(MI, LoweredMI, SystemZ::VSTMAlign);
569 break;
570
571 case SystemZ::VL16:
572 LoweredMI = lowerSubvectorLoad(MI, SystemZ::VLREPH);
573 break;
574
575 case SystemZ::VL32:
576 LoweredMI = lowerSubvectorLoad(MI, SystemZ::VLREPF);
577 break;
578
579 case SystemZ::VL64:
580 LoweredMI = lowerSubvectorLoad(MI, SystemZ::VLREPG);
581 break;
582
583 case SystemZ::VST16:
584 LoweredMI = lowerSubvectorStore(MI, SystemZ::VSTEH);
585 break;
586
587 case SystemZ::VST32:
588 LoweredMI = lowerSubvectorStore(MI, SystemZ::VSTEF);
589 break;
590
591 case SystemZ::VST64:
592 LoweredMI = lowerSubvectorStore(MI, SystemZ::VSTEG);
593 break;
594
595 case SystemZ::LFER:
596 LoweredMI = lowerVecEltExtraction(MI, SystemZ::VLGVF);
597 break;
598
599 case SystemZ::LFER_16:
600 LoweredMI = lowerVecEltExtraction(MI, SystemZ::VLGVH);
601 break;
602
603 case SystemZ::LEFR:
604 LoweredMI = lowerVecEltInsertion(MI, SystemZ::VLVGF);
605 break;
606
607 case SystemZ::LEFR_16:
608 LoweredMI = lowerVecEltInsertion(MI, SystemZ::VLVGH);
609 break;
610
611#define LOWER_LOW(NAME) \
612 case SystemZ::NAME##64: LoweredMI = lowerRILow(MI, SystemZ::NAME); break
613
614 LOWER_LOW(IILL);
615 LOWER_LOW(IILH);
616 LOWER_LOW(TMLL);
617 LOWER_LOW(TMLH);
618 LOWER_LOW(NILL);
619 LOWER_LOW(NILH);
620 LOWER_LOW(NILF);
621 LOWER_LOW(OILL);
622 LOWER_LOW(OILH);
623 LOWER_LOW(OILF);
624 LOWER_LOW(XILF);
625
626#undef LOWER_LOW
627
628#define LOWER_HIGH(NAME) \
629 case SystemZ::NAME##64: LoweredMI = lowerRIHigh(MI, SystemZ::NAME); break
630
631 LOWER_HIGH(IIHL);
632 LOWER_HIGH(IIHH);
633 LOWER_HIGH(TMHL);
634 LOWER_HIGH(TMHH);
635 LOWER_HIGH(NIHL);
636 LOWER_HIGH(NIHH);
637 LOWER_HIGH(NIHF);
638 LOWER_HIGH(OIHL);
639 LOWER_HIGH(OIHH);
640 LOWER_HIGH(OIHF);
641 LOWER_HIGH(XIHF);
642
643#undef LOWER_HIGH
644
645 case SystemZ::Serialize:
646 if (MF->getSubtarget<SystemZSubtarget>().hasFastSerialization())
647 LoweredMI = MCInstBuilder(SystemZ::BCRAsm)
648 .addImm(14).addReg(SystemZ::R0D);
649 else
650 LoweredMI = MCInstBuilder(SystemZ::BCRAsm)
651 .addImm(15).addReg(SystemZ::R0D);
652 break;
653
654 // We want to emit "j .+2" for traps, jumping to the relative immediate field
655 // of the jump instruction, which is an illegal instruction. We cannot emit a
656 // "." symbol, so create and emit a temp label before the instruction and use
657 // that instead.
658 case SystemZ::Trap: {
659 MCSymbol *DotSym = OutContext.createTempSymbol();
660 OutStreamer->emitLabel(DotSym);
661
663 const MCConstantExpr *ConstExpr = MCConstantExpr::create(2, OutContext);
664 LoweredMI = MCInstBuilder(SystemZ::J)
665 .addExpr(MCBinaryExpr::createAdd(Expr, ConstExpr, OutContext));
666 }
667 break;
668
669 // Conditional traps will create a branch on condition instruction that jumps
670 // to the relative immediate field of the jump instruction. (eg. "jo .+2")
671 case SystemZ::CondTrap: {
672 MCSymbol *DotSym = OutContext.createTempSymbol();
673 OutStreamer->emitLabel(DotSym);
674
676 const MCConstantExpr *ConstExpr = MCConstantExpr::create(2, OutContext);
677 LoweredMI = MCInstBuilder(SystemZ::BRC)
678 .addImm(MI->getOperand(0).getImm())
679 .addImm(MI->getOperand(1).getImm())
680 .addExpr(MCBinaryExpr::createAdd(Expr, ConstExpr, OutContext));
681 }
682 break;
683
684 case TargetOpcode::FENTRY_CALL:
685 LowerFENTRY_CALL(*MI, Lower);
686 return;
687
688 case TargetOpcode::STACKMAP:
689 LowerSTACKMAP(*MI);
690 return;
691
692 case TargetOpcode::PATCHPOINT:
693 LowerPATCHPOINT(*MI, Lower);
694 return;
695
696 case TargetOpcode::PATCHABLE_FUNCTION_ENTER:
697 LowerPATCHABLE_FUNCTION_ENTER(*MI, Lower);
698 return;
699
700 case TargetOpcode::PATCHABLE_RET:
701 LowerPATCHABLE_RET(*MI, Lower);
702 return;
703
704 case TargetOpcode::PATCHABLE_FUNCTION_EXIT:
705 llvm_unreachable("PATCHABLE_FUNCTION_EXIT should never be emitted");
706
707 case TargetOpcode::PATCHABLE_TAIL_CALL:
708 // TODO: Define a trampoline `__xray_FunctionTailExit` and differentiate a
709 // normal function exit from a tail exit.
710 llvm_unreachable("Tail call is handled in the normal case. See comments "
711 "around this assert.");
712
713 case SystemZ::EXRL_Pseudo: {
714 unsigned TargetInsOpc = MI->getOperand(0).getImm();
715 Register LenMinus1Reg = MI->getOperand(1).getReg();
716 Register DestReg = MI->getOperand(2).getReg();
717 int64_t DestDisp = MI->getOperand(3).getImm();
718 Register SrcReg = MI->getOperand(4).getReg();
719 int64_t SrcDisp = MI->getOperand(5).getImm();
720
721 SystemZTargetStreamer *TS = getTargetStreamer();
722 MCInst ET = MCInstBuilder(TargetInsOpc)
723 .addReg(DestReg)
724 .addImm(DestDisp)
725 .addImm(1)
726 .addReg(SrcReg)
727 .addImm(SrcDisp);
728 SystemZTargetStreamer::MCInstSTIPair ET_STI(ET, &MF->getSubtarget());
729 auto [It, Inserted] = TS->EXRLTargets2Sym.try_emplace(ET_STI);
730 if (Inserted)
731 It->second = OutContext.createTempSymbol();
732 MCSymbol *DotSym = It->second;
736 MCInstBuilder(SystemZ::EXRL).addReg(LenMinus1Reg).addExpr(Dot));
737 return;
738 }
739
740 // EH_SjLj_Setup is a dummy terminator instruction of size 0.
741 // It is used to handle the clobber register for builtin setjmp.
742 case SystemZ::EH_SjLj_Setup:
743 return;
744
745 default:
746 Lower.lower(MI, LoweredMI);
747 break;
748 }
749 EmitToStreamer(*OutStreamer, LoweredMI);
750}
751
752// Emit the largest nop instruction smaller than or equal to NumBytes
753// bytes. Return the size of nop emitted.
755 unsigned NumBytes, const MCSubtargetInfo &STI) {
756 if (NumBytes < 2) {
757 llvm_unreachable("Zero nops?");
758 return 0;
759 }
760 else if (NumBytes < 4) {
761 OutStreamer.emitInstruction(
762 MCInstBuilder(SystemZ::BCRAsm).addImm(0).addReg(SystemZ::R0D), STI);
763 return 2;
764 }
765 else if (NumBytes < 6) {
766 OutStreamer.emitInstruction(
767 MCInstBuilder(SystemZ::BCAsm).addImm(0).addReg(0).addImm(0).addReg(0),
768 STI);
769 return 4;
770 }
771 else {
774 OutStreamer.emitLabel(DotSym);
775 OutStreamer.emitInstruction(
776 MCInstBuilder(SystemZ::BRCLAsm).addImm(0).addExpr(Dot), STI);
777 return 6;
778 }
779}
780
781void SystemZAsmPrinter::LowerFENTRY_CALL(const MachineInstr &MI,
782 SystemZMCInstLower &Lower) {
783 MCContext &Ctx = MF->getContext();
784 if (MF->getFunction().hasFnAttribute("mrecord-mcount")) {
785 MCSymbol *DotSym = OutContext.createTempSymbol();
786 OutStreamer->pushSection();
787 OutStreamer->switchSection(
788 Ctx.getELFSection("__mcount_loc", ELF::SHT_PROGBITS, ELF::SHF_ALLOC));
789 OutStreamer->emitSymbolValue(DotSym, 8);
790 OutStreamer->popSection();
791 OutStreamer->emitLabel(DotSym);
792 }
793
794 if (MF->getFunction().hasFnAttribute("mnop-mcount")) {
796 return;
797 }
798
799 MCSymbol *fentry = Ctx.getOrCreateSymbol("__fentry__");
800 const MCSymbolRefExpr *Op =
802 OutStreamer->emitInstruction(
803 MCInstBuilder(SystemZ::BRASL).addReg(SystemZ::R0D).addExpr(Op),
805}
806
807void SystemZAsmPrinter::LowerSTACKMAP(const MachineInstr &MI) {
808 auto *TII = MF->getSubtarget<SystemZSubtarget>().getInstrInfo();
809
810 unsigned NumNOPBytes = MI.getOperand(1).getImm();
811
812 auto &Ctx = OutStreamer->getContext();
813 MCSymbol *MILabel = Ctx.createTempSymbol();
814 OutStreamer->emitLabel(MILabel);
815
816 SM.recordStackMap(*MILabel, MI);
817 assert(NumNOPBytes % 2 == 0 && "Invalid number of NOP bytes requested!");
818
819 // Scan ahead to trim the shadow.
820 unsigned ShadowBytes = 0;
821 const MachineBasicBlock &MBB = *MI.getParent();
823 ++MII;
824 while (ShadowBytes < NumNOPBytes) {
825 if (MII == MBB.end() ||
826 MII->getOpcode() == TargetOpcode::PATCHPOINT ||
827 MII->getOpcode() == TargetOpcode::STACKMAP)
828 break;
829 ShadowBytes += TII->getInstSizeInBytes(*MII);
830 if (MII->isCall())
831 break;
832 ++MII;
833 }
834
835 // Emit nops.
836 while (ShadowBytes < NumNOPBytes)
837 ShadowBytes += EmitNop(OutContext, *OutStreamer, NumNOPBytes - ShadowBytes,
839}
840
841// Lower a patchpoint of the form:
842// [<def>], <id>, <numBytes>, <target>, <numArgs>
843void SystemZAsmPrinter::LowerPATCHPOINT(const MachineInstr &MI,
844 SystemZMCInstLower &Lower) {
845 auto &Ctx = OutStreamer->getContext();
846 MCSymbol *MILabel = Ctx.createTempSymbol();
847 OutStreamer->emitLabel(MILabel);
848
849 SM.recordPatchPoint(*MILabel, MI);
850 PatchPointOpers Opers(&MI);
851
852 unsigned EncodedBytes = 0;
853 const MachineOperand &CalleeMO = Opers.getCallTarget();
854
855 if (CalleeMO.isImm()) {
856 uint64_t CallTarget = CalleeMO.getImm();
857 if (CallTarget) {
858 unsigned ScratchIdx = -1;
859 unsigned ScratchReg = 0;
860 do {
861 ScratchIdx = Opers.getNextScratchIdx(ScratchIdx + 1);
862 ScratchReg = MI.getOperand(ScratchIdx).getReg();
863 } while (ScratchReg == SystemZ::R0D);
864
865 // Materialize the call target address
866 EmitToStreamer(*OutStreamer, MCInstBuilder(SystemZ::LLILF)
867 .addReg(ScratchReg)
868 .addImm(CallTarget & 0xFFFFFFFF));
869 EncodedBytes += 6;
870 if (CallTarget >> 32) {
871 EmitToStreamer(*OutStreamer, MCInstBuilder(SystemZ::IIHF)
872 .addReg(ScratchReg)
873 .addImm(CallTarget >> 32));
874 EncodedBytes += 6;
875 }
876
877 EmitToStreamer(*OutStreamer, MCInstBuilder(SystemZ::BASR)
878 .addReg(SystemZ::R14D)
879 .addReg(ScratchReg));
880 EncodedBytes += 2;
881 }
882 } else if (CalleeMO.isGlobal()) {
883 const MCExpr *Expr = Lower.getExpr(CalleeMO, SystemZ::S_PLT);
884 EmitToStreamer(*OutStreamer, MCInstBuilder(SystemZ::BRASL)
885 .addReg(SystemZ::R14D)
886 .addExpr(Expr));
887 EncodedBytes += 6;
888 }
889
890 // Emit padding.
891 unsigned NumBytes = Opers.getNumPatchBytes();
892 assert(NumBytes >= EncodedBytes &&
893 "Patchpoint can't request size less than the length of a call.");
894 assert((NumBytes - EncodedBytes) % 2 == 0 &&
895 "Invalid number of NOP bytes requested!");
896 while (EncodedBytes < NumBytes)
897 EncodedBytes += EmitNop(OutContext, *OutStreamer, NumBytes - EncodedBytes,
899}
900
901void SystemZAsmPrinter::LowerPATCHABLE_FUNCTION_ENTER(
902 const MachineInstr &MI, SystemZMCInstLower &Lower) {
903
904 const MachineFunction &MF = *(MI.getParent()->getParent());
905 const Function &F = MF.getFunction();
906
907 // If patchable-function-entry is set, emit in-function nops here.
908 if (F.hasFnAttribute("patchable-function-entry")) {
909 unsigned Num;
910 // get M-N from function attribute (CodeGenFunction subtracts N
911 // from M to yield the correct patchable-function-entry).
912 if (F.getFnAttribute("patchable-function-entry")
913 .getValueAsString()
914 .getAsInteger(10, Num))
915 return;
916 // Emit M-N 2-byte nops. Use getNop() here instead of emitNops()
917 // to keep it aligned with the common code implementation emitting
918 // the prefix nops.
919 for (unsigned I = 0; I < Num; ++I)
920 EmitToStreamer(*OutStreamer, MF.getSubtarget().getInstrInfo()->getNop());
921 return;
922 }
923 // Otherwise, emit xray sled.
924 // .begin:
925 // j .end # -> stmg %r2, %r15, 16(%r15)
926 // nop
927 // llilf %2, FuncID
928 // brasl %r14, __xray_FunctionEntry@GOT
929 // .end:
930 //
931 // Update compiler-rt/lib/xray/xray_s390x.cpp accordingly when number
932 // of instructions change.
933 bool HasVectorFeature =
934 TM.getMCSubtargetInfo()->hasFeature(SystemZ::FeatureVector) &&
935 !TM.getMCSubtargetInfo()->hasFeature(SystemZ::FeatureSoftFloat);
936 MCSymbol *FuncEntry = OutContext.getOrCreateSymbol(
937 HasVectorFeature ? "__xray_FunctionEntryVec" : "__xray_FunctionEntry");
938 MCSymbol *BeginOfSled = OutContext.createTempSymbol("xray_sled_", true);
939 MCSymbol *EndOfSled = OutContext.createTempSymbol();
940 OutStreamer->emitLabel(BeginOfSled);
942 MCInstBuilder(SystemZ::J)
943 .addExpr(MCSymbolRefExpr::create(EndOfSled, OutContext)));
946 MCInstBuilder(SystemZ::LLILF).addReg(SystemZ::R2D).addImm(0));
947 EmitToStreamer(*OutStreamer, MCInstBuilder(SystemZ::BRASL)
948 .addReg(SystemZ::R14D)
950 FuncEntry, SystemZ::S_PLT, OutContext)));
951 OutStreamer->emitLabel(EndOfSled);
952 recordSled(BeginOfSled, MI, SledKind::FUNCTION_ENTER, 2);
953}
954
955void SystemZAsmPrinter::LowerPATCHABLE_RET(const MachineInstr &MI,
956 SystemZMCInstLower &Lower) {
957 unsigned OpCode = MI.getOperand(0).getImm();
958 MCSymbol *FallthroughLabel = nullptr;
959 if (OpCode == SystemZ::CondReturn) {
960 FallthroughLabel = OutContext.createTempSymbol();
961 int64_t Cond0 = MI.getOperand(1).getImm();
962 int64_t Cond1 = MI.getOperand(2).getImm();
963 EmitToStreamer(*OutStreamer, MCInstBuilder(SystemZ::BRC)
964 .addImm(Cond0)
965 .addImm(Cond1 ^ Cond0)
967 FallthroughLabel, OutContext)));
968 }
969 // .begin:
970 // br %r14 # -> stmg %r2, %r15, 24(%r15)
971 // nop
972 // nop
973 // llilf %2,FuncID
974 // j __xray_FunctionExit@GOT
975 //
976 // Update compiler-rt/lib/xray/xray_s390x.cpp accordingly when number
977 // of instructions change.
978 bool HasVectorFeature =
979 TM.getMCSubtargetInfo()->hasFeature(SystemZ::FeatureVector) &&
980 !TM.getMCSubtargetInfo()->hasFeature(SystemZ::FeatureSoftFloat);
981 MCSymbol *FuncExit = OutContext.getOrCreateSymbol(
982 HasVectorFeature ? "__xray_FunctionExitVec" : "__xray_FunctionExit");
983 MCSymbol *BeginOfSled = OutContext.createTempSymbol("xray_sled_", true);
984 OutStreamer->emitLabel(BeginOfSled);
986 MCInstBuilder(SystemZ::BR).addReg(SystemZ::R14D));
989 MCInstBuilder(SystemZ::LLILF).addReg(SystemZ::R2D).addImm(0));
990 EmitToStreamer(*OutStreamer, MCInstBuilder(SystemZ::J)
992 FuncExit, SystemZ::S_PLT, OutContext)));
993 if (FallthroughLabel)
994 OutStreamer->emitLabel(FallthroughLabel);
995 recordSled(BeginOfSled, MI, SledKind::FUNCTION_EXIT, 2);
996}
997
998// The *alignment* of 128-bit vector types is different between the software
999// and hardware vector ABIs. If the there is an externally visible use of a
1000// vector type in the module it should be annotated with an attribute.
1001void SystemZAsmPrinter::emitAttributes(Module &M) {
1002 if (M.getModuleFlag("s390x-visible-vector-ABI")) {
1003 bool HasVectorFeature =
1004 TM.getMCSubtargetInfo()->hasFeature(SystemZ::FeatureVector);
1005 OutStreamer->emitGNUAttribute(8, HasVectorFeature ? 2 : 1);
1006 }
1007}
1008
1009// Convert a SystemZ-specific constant pool modifier into the associated
1010// specifier.
1012 switch (Modifier) {
1013 case SystemZCP::TLSGD:
1014 return SystemZ::S_TLSGD;
1015 case SystemZCP::TLSLDM:
1016 return SystemZ::S_TLSLDM;
1017 case SystemZCP::DTPOFF:
1018 return SystemZ::S_DTPOFF;
1019 case SystemZCP::NTPOFF:
1020 return SystemZ::S_NTPOFF;
1021 }
1022 llvm_unreachable("Invalid SystemCPModifier!");
1023}
1024
1027 auto *ZCPV = static_cast<SystemZConstantPoolValue*>(MCPV);
1028
1029 const MCExpr *Expr = MCSymbolRefExpr::create(
1030 getSymbol(ZCPV->getGlobalValue()),
1031 getSpecifierFromModifier(ZCPV->getModifier()), OutContext);
1032 uint64_t Size = getDataLayout().getTypeAllocSize(ZCPV->getType());
1033
1034 OutStreamer->emitValue(Expr, Size);
1035}
1036
1037// Emit the ctor or dtor list taking into account the init priority.
1039 const Constant *List, bool IsCtor) {
1040 if (!TM.getTargetTriple().isOSBinFormatGOFF())
1041 return AsmPrinter::emitXXStructorList(DL, List, IsCtor);
1042
1043 SmallVector<Structor, 8> Structors;
1044 preprocessXXStructorList(DL, List, Structors);
1045 if (Structors.empty())
1046 return;
1047
1048 const Align Align = llvm::Align(4);
1049 const TargetLoweringObjectFileGOFF &Obj =
1050 static_cast<const TargetLoweringObjectFileGOFF &>(getObjFileLowering());
1051 for (Structor &S : Structors) {
1052 MCSectionGOFF *Section =
1053 static_cast<MCSectionGOFF *>(Obj.getStaticXtorSection(S.Priority));
1054 OutStreamer->switchSection(Section);
1055 if (OutStreamer->getCurrentSection() != OutStreamer->getPreviousSection())
1057
1058 // The priority is provided as an input to getStaticXtorSection(), and is
1059 // recalculated within that function as `Prio` going to going into the
1060 // PR section.
1061 // This priority retrieved via the `SortKey` below is the recalculated
1062 // Priority.
1063 uint32_t XtorPriority = Section->getPRAttributes().SortKey;
1064
1065 const GlobalValue *GV = dyn_cast<GlobalValue>(S.Func->stripPointerCasts());
1066 assert(GV && "C++ xxtor pointer was not a GlobalValue!");
1067 MCSymbolGOFF *Symbol = static_cast<MCSymbolGOFF *>(getSymbol(GV));
1068
1069 // @@SQINIT entry: { unsigned prio; void (*ctor)(); void (*dtor)(); }
1070
1071 unsigned PointerSizeInBytes = DL.getPointerSize();
1072
1073 auto &Ctx = OutStreamer->getContext();
1074 const MCExpr *ADAFuncRefExpr;
1075 unsigned SlotKind = SystemZII::MO_ADA_DIRECT_FUNC_DESC;
1076
1077 MCSectionGOFF *ADASection =
1078 static_cast<MCSectionGOFF *>(Obj.getADASection());
1079 assert(ADASection && "ADA section must exist for GOFF targets!");
1080 const MCSymbol *ADASym = ADASection->getBeginSymbol();
1081 assert(ADASym && "ADA symbol should already be set!");
1082
1083 ADAFuncRefExpr = MCBinaryExpr::createAdd(
1086 MCConstantExpr::create(ADATable.insert(Symbol, SlotKind), Ctx), Ctx);
1087
1088 emitInt32(XtorPriority);
1089 if (IsCtor) {
1090 OutStreamer->emitValue(ADAFuncRefExpr, PointerSizeInBytes);
1091 OutStreamer->emitIntValue(0, PointerSizeInBytes);
1092 } else {
1093 OutStreamer->emitIntValue(0, PointerSizeInBytes);
1094 OutStreamer->emitValue(ADAFuncRefExpr, PointerSizeInBytes);
1095 }
1096 }
1097}
1098
1099static void printFormattedRegName(const MCAsmInfo *MAI, unsigned RegNo,
1100 raw_ostream &OS) {
1101 const char *RegName;
1102 if (MAI->getAssemblerDialect() == AD_HLASM) {
1104 // Skip register prefix so that only register number is left
1105 assert(isalpha(RegName[0]) && isdigit(RegName[1]));
1106 OS << (RegName + 1);
1107 } else {
1109 OS << '%' << RegName;
1110 }
1111}
1112
1113static void printReg(unsigned Reg, const MCAsmInfo *MAI, raw_ostream &OS) {
1114 if (!Reg)
1115 OS << '0';
1116 else
1118}
1119
1120static void printOperand(const MCOperand &MCOp, const MCAsmInfo *MAI,
1121 raw_ostream &OS) {
1122 if (MCOp.isReg())
1123 printReg(MCOp.getReg(), MAI, OS);
1124 else if (MCOp.isImm())
1125 OS << MCOp.getImm();
1126 else if (MCOp.isExpr())
1127 MAI->printExpr(OS, *MCOp.getExpr());
1128 else
1129 llvm_unreachable("Invalid operand");
1130}
1131
1132static void printAddress(const MCAsmInfo *MAI, unsigned Base,
1133 const MCOperand &DispMO, unsigned Index,
1134 raw_ostream &OS) {
1135 printOperand(DispMO, MAI, OS);
1136 if (Base || Index) {
1137 OS << '(';
1138 if (Index) {
1139 printFormattedRegName(MAI, Index, OS);
1140 if (Base)
1141 OS << ',';
1142 }
1143 if (Base)
1145 OS << ')';
1146 }
1147}
1148
1150 const char *ExtraCode,
1151 raw_ostream &OS) {
1152 const MCRegisterInfo &MRI = *TM.getMCRegisterInfo();
1153 const MachineOperand &MO = MI->getOperand(OpNo);
1154 MCOperand MCOp;
1155 if (ExtraCode) {
1156 if (ExtraCode[0] == 'N' && !ExtraCode[1] && MO.isReg() &&
1157 SystemZ::GR128BitRegClass.contains(MO.getReg()))
1158 MCOp =
1159 MCOperand::createReg(MRI.getSubReg(MO.getReg(), SystemZ::subreg_l64));
1160 else
1161 return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS);
1162 } else {
1163 SystemZMCInstLower Lower(MF->getContext(), *this);
1164 MCOp = Lower.lowerOperand(MO);
1165 }
1166 printOperand(MCOp, MAI, OS);
1167 return false;
1168}
1169
1171 unsigned OpNo,
1172 const char *ExtraCode,
1173 raw_ostream &OS) {
1174 if (ExtraCode && ExtraCode[0] && !ExtraCode[1]) {
1175 switch (ExtraCode[0]) {
1176 case 'A':
1177 // Unlike EmitMachineNode(), EmitSpecialNode(INLINEASM) does not call
1178 // setMemRefs(), so MI->memoperands() is empty and the alignment
1179 // information is not available.
1180 return false;
1181 case 'O':
1182 OS << MI->getOperand(OpNo + 1).getImm();
1183 return false;
1184 case 'R':
1185 ::printReg(MI->getOperand(OpNo).getReg(), MAI, OS);
1186 return false;
1187 }
1188 }
1189 printAddress(MAI, MI->getOperand(OpNo).getReg(),
1190 MCOperand::createImm(MI->getOperand(OpNo + 1).getImm()),
1191 MI->getOperand(OpNo + 2).getReg(), OS);
1192 return false;
1193}
1194
1196 auto TT = OutContext.getTargetTriple();
1197 if (TT.isOSzOS()) {
1198 emitADASection();
1199 emitIDRLSection(M);
1200 }
1201 emitAttributes(M);
1202}
1203
1204void SystemZAsmPrinter::emitADASection() {
1205 OutStreamer->pushSection();
1206
1207 const unsigned PointerSize = getDataLayout().getPointerSize();
1208 OutStreamer->switchSection(getObjFileLowering().getADASection());
1209
1210 unsigned EmittedBytes = 0;
1211 for (auto &Entry : ADATable.getTable()) {
1212 const MCSymbol *Sym;
1213 unsigned SlotKind;
1214 std::tie(Sym, SlotKind) = Entry.first;
1215 unsigned Offset = Entry.second;
1216 assert(Offset == EmittedBytes && "Offset not as expected");
1217 (void)EmittedBytes;
1218#define EMIT_COMMENT(Str) \
1219 OutStreamer->AddComment(Twine("Offset ") \
1220 .concat(utostr(Offset)) \
1221 .concat(" " Str " ") \
1222 .concat(Sym->getName()));
1223 switch (SlotKind) {
1225 // Language Environment DLL logic requires function descriptors, for
1226 // imported functions, that are placed in the ADA to be 8 byte aligned.
1227 EMIT_COMMENT("function descriptor of");
1228 OutStreamer->emitValue(
1231 PointerSize);
1232 OutStreamer->emitValue(
1235 PointerSize);
1236 EmittedBytes += PointerSize * 2;
1237 break;
1239 EMIT_COMMENT("pointer to data symbol");
1240 OutStreamer->emitValue(
1243 PointerSize);
1244 EmittedBytes += PointerSize;
1245 break;
1248 Twine(Sym->getName()).concat("@indirect"));
1249 OutStreamer->emitAssignment(Alias,
1251 OutStreamer->emitSymbolAttribute(Alias, MCSA_IndirectSymbol);
1252
1253 EMIT_COMMENT("pointer to function descriptor");
1254 OutStreamer->emitValue(
1257 PointerSize);
1258 EmittedBytes += PointerSize;
1259 break;
1260 }
1261 default:
1262 llvm_unreachable("Unexpected slot kind");
1263 }
1264#undef EMIT_COMMENT
1265 }
1266 OutStreamer->popSection();
1267}
1268
1269static std::string getProductID(Module &M) {
1270 std::string ProductID;
1271 if (auto *MD = M.getModuleFlag("zos_product_id"))
1272 ProductID = cast<MDString>(MD)->getString().str();
1273 if (ProductID.empty())
1274 ProductID = "LLVM";
1275 return ProductID;
1276}
1277
1279 if (auto *VersionVal = mdconst::extract_or_null<ConstantInt>(
1280 M.getModuleFlag("zos_product_major_version")))
1281 return VersionVal->getZExtValue();
1282 return LLVM_VERSION_MAJOR;
1283}
1284
1286 if (auto *ReleaseVal = mdconst::extract_or_null<ConstantInt>(
1287 M.getModuleFlag("zos_product_minor_version")))
1288 return ReleaseVal->getZExtValue();
1289 return LLVM_VERSION_MINOR;
1290}
1291
1293 if (auto *PatchVal = mdconst::extract_or_null<ConstantInt>(
1294 M.getModuleFlag("zos_product_patchlevel")))
1295 return PatchVal->getZExtValue();
1296 return LLVM_VERSION_PATCH;
1297}
1298
1299static time_t getTranslationTime(Module &M) {
1300 std::time_t Time = 0;
1302 M.getModuleFlag("zos_translation_time"))) {
1303 long SecondsSinceEpoch = Val->getSExtValue();
1304 Time = static_cast<time_t>(SecondsSinceEpoch);
1305 }
1306 return Time;
1307}
1308
1309void SystemZAsmPrinter::emitIDRLSection(Module &M) {
1310 OutStreamer->pushSection();
1311 OutStreamer->switchSection(getObjFileLowering().getIDRLSection());
1312 constexpr unsigned IDRLDataLength = 30;
1313 std::time_t Time = getTranslationTime(M);
1314
1315 uint32_t ProductVersion = getProductVersion(M);
1316 uint32_t ProductRelease = getProductRelease(M);
1317
1318 std::string ProductID = getProductID(M);
1319
1320 SmallString<IDRLDataLength + 1> TempStr;
1321 raw_svector_ostream O(TempStr);
1322 O << formatv("{0,-10}{1,0-2:d}{2,0-2:d}{3:%Y%m%d%H%M%S}{4,0-2}",
1323 ProductID.substr(0, 10).c_str(), ProductVersion, ProductRelease,
1324 llvm::sys::toUtcTime(Time), "0");
1325 SmallString<IDRLDataLength> Data;
1327
1328 OutStreamer->emitInt8(0); // Reserved.
1329 OutStreamer->emitInt8(3); // Format.
1330 OutStreamer->emitInt16(IDRLDataLength); // Length.
1331 OutStreamer->emitBytes(Data.str());
1332 OutStreamer->popSection();
1333}
1334
1336 if (TM.getTargetTriple().isOSzOS()) {
1337 // Emit symbol for the end of function if the z/OS target streamer
1338 // is used. This is needed to calculate the size of the function.
1339 MCSymbol *FnEndSym = createTempSymbol("func_end");
1340 OutStreamer->emitLabel(FnEndSym);
1341
1342 OutStreamer->pushSection();
1343 OutStreamer->switchSection(getObjFileLowering().getTextSection(),
1345 emitPPA1(FnEndSym);
1346 OutStreamer->popSection();
1347
1348 CurrentFnPPA1Sym = nullptr;
1349 CurrentFnEPMarkerSym = nullptr;
1350 }
1351}
1352
1353static void emitPPA1Flags(std::unique_ptr<MCStreamer> &OutStreamer, bool VarArg,
1354 bool StackProtector, bool FPRMask, bool VRMask,
1355 bool EHBlock, bool HasArgAreaLength, bool HasName) {
1356 enum class PPA1Flag1 : uint8_t {
1357 DSA64Bit = (0x80 >> 0),
1358 VarArg = (0x80 >> 7),
1360 };
1361 enum class PPA1Flag2 : uint8_t {
1362 ExternalProcedure = (0x80 >> 0),
1363 STACKPROTECTOR = (0x80 >> 3),
1364 LLVM_MARK_AS_BITMASK_ENUM(ExternalProcedure)
1365 };
1366 enum class PPA1Flag3 : uint8_t {
1367 HasArgAreaLength = (0x80 >> 1),
1368 FPRMask = (0x80 >> 2),
1369 LLVM_MARK_AS_BITMASK_ENUM(HasArgAreaLength)
1370 };
1371 enum class PPA1Flag4 : uint8_t {
1372 EPMOffsetPresent = (0x80 >> 0),
1373 VRMask = (0x80 >> 2),
1374 EHBlock = (0x80 >> 3),
1375 ProcedureNamePresent = (0x80 >> 7),
1376 LLVM_MARK_AS_BITMASK_ENUM(EPMOffsetPresent)
1377 };
1378
1379 // Declare optional section flags that can be modified.
1380 auto Flags1 = PPA1Flag1(0);
1381 auto Flags2 = PPA1Flag2::ExternalProcedure;
1382 auto Flags3 = PPA1Flag3(0);
1383 auto Flags4 = PPA1Flag4::EPMOffsetPresent;
1384
1385 Flags1 |= PPA1Flag1::DSA64Bit;
1386
1387 if (VarArg)
1388 Flags1 |= PPA1Flag1::VarArg;
1389
1390 if (StackProtector)
1391 Flags2 |= PPA1Flag2::STACKPROTECTOR;
1392
1393 if (HasArgAreaLength)
1394 Flags3 |= PPA1Flag3::HasArgAreaLength; // Add emit ArgAreaLength flag.
1395
1396 // SavedGPRMask, SavedFPRMask, and SavedVRMask are precomputed in.
1397 if (FPRMask)
1398 Flags3 |= PPA1Flag3::FPRMask; // Add emit FPR mask flag.
1399
1400 if (VRMask)
1401 Flags4 |= PPA1Flag4::VRMask; // Add emit VR mask flag.
1402
1403 if (EHBlock)
1404 Flags4 |= PPA1Flag4::EHBlock; // Add optional EH block.
1405
1406 if (HasName)
1407 Flags4 |= PPA1Flag4::ProcedureNamePresent; // Add optional name block.
1408
1409 OutStreamer->AddComment("PPA1 Flags 1");
1410 if ((Flags1 & PPA1Flag1::DSA64Bit) == PPA1Flag1::DSA64Bit)
1411 OutStreamer->AddComment(" Bit 0: 1 = 64-bit DSA");
1412 else
1413 OutStreamer->AddComment(" Bit 0: 0 = 32-bit DSA");
1414 if ((Flags1 & PPA1Flag1::VarArg) == PPA1Flag1::VarArg)
1415 OutStreamer->AddComment(" Bit 7: 1 = Vararg function");
1416 OutStreamer->emitInt8(static_cast<uint8_t>(Flags1)); // Flags 1.
1417
1418 OutStreamer->AddComment("PPA1 Flags 2");
1419 if ((Flags2 & PPA1Flag2::ExternalProcedure) == PPA1Flag2::ExternalProcedure)
1420 OutStreamer->AddComment(" Bit 0: 1 = External procedure");
1421 if ((Flags2 & PPA1Flag2::STACKPROTECTOR) == PPA1Flag2::STACKPROTECTOR)
1422 OutStreamer->AddComment(" Bit 3: 1 = STACKPROTECT is enabled");
1423 else
1424 OutStreamer->AddComment(" Bit 3: 0 = STACKPROTECT is not enabled");
1425 OutStreamer->emitInt8(static_cast<uint8_t>(Flags2)); // Flags 2.
1426
1427 OutStreamer->AddComment("PPA1 Flags 3");
1428 if ((Flags3 & PPA1Flag3::HasArgAreaLength) == PPA1Flag3::HasArgAreaLength)
1429 OutStreamer->AddComment(
1430 " Bit 1: 1 = Argument Area Length is in optional area");
1431 if ((Flags3 & PPA1Flag3::FPRMask) == PPA1Flag3::FPRMask)
1432 OutStreamer->AddComment(" Bit 2: 1 = FP Reg Mask is in optional area");
1433 OutStreamer->emitInt8(
1434 static_cast<uint8_t>(Flags3)); // Flags 3 (optional sections).
1435
1436 OutStreamer->AddComment("PPA1 Flags 4");
1437 if ((Flags4 & PPA1Flag4::VRMask) == PPA1Flag4::VRMask)
1438 OutStreamer->AddComment(" Bit 2: 1 = Vector Reg Mask is in optional area");
1439 if ((Flags4 & PPA1Flag4::EHBlock) == PPA1Flag4::EHBlock)
1440 OutStreamer->AddComment(" Bit 3: 1 = C++ EH block");
1441 if ((Flags4 & PPA1Flag4::ProcedureNamePresent) ==
1442 PPA1Flag4::ProcedureNamePresent)
1443 OutStreamer->AddComment(" Bit 7: 1 = Name Length and Name");
1444 OutStreamer->emitInt8(static_cast<uint8_t>(
1445 Flags4)); // Flags 4 (optional sections, always emit these).
1446}
1447
1448static void emitPPA1Name(std::unique_ptr<MCStreamer> &OutStreamer,
1449 StringRef OutName) {
1450 size_t NameSize = OutName.size();
1451 uint16_t OutSize;
1452 if (NameSize < UINT16_MAX) {
1453 OutSize = static_cast<uint16_t>(NameSize);
1454 } else {
1455 OutName = OutName.substr(0, UINT16_MAX);
1456 OutSize = UINT16_MAX;
1457 }
1458 // Emit padding to ensure that the next optional field word-aligned.
1459 uint8_t ExtraZeros = 4 - ((2 + OutSize) % 4);
1460
1461 SmallString<512> OutnameConv;
1462 ConverterEBCDIC::convertToEBCDIC(OutName, OutnameConv);
1463 OutName = OutnameConv.str();
1464
1465 OutStreamer->AddComment("Length of Name");
1466 OutStreamer->emitInt16(OutSize);
1467 OutStreamer->AddComment("Name of Function");
1468 OutStreamer->emitBytes(OutName);
1469 OutStreamer->emitZeros(ExtraZeros);
1470}
1471
1472void SystemZAsmPrinter::emitPPA1(MCSymbol *FnEndSym) {
1473 assert(PPA2Sym != nullptr && "PPA2 Symbol not defined");
1474
1475 const TargetRegisterInfo *TRI = MF->getRegInfo().getTargetRegisterInfo();
1476 const SystemZSubtarget &Subtarget = MF->getSubtarget<SystemZSubtarget>();
1477 const auto TargetHasVector = Subtarget.hasVector();
1478
1479 const SystemZMachineFunctionInfo *ZFI =
1480 MF->getInfo<SystemZMachineFunctionInfo>();
1481 const auto *ZFL = static_cast<const SystemZXPLINKFrameLowering *>(
1482 Subtarget.getFrameLowering());
1483 const MachineFrameInfo &MFFrame = MF->getFrameInfo();
1484
1485 // Get saved GPR/FPR/VPR masks.
1486 const std::vector<CalleeSavedInfo> &CSI = MFFrame.getCalleeSavedInfo();
1487 uint16_t SavedGPRMask = 0;
1488 uint16_t SavedFPRMask = 0;
1489 uint8_t SavedVRMask = 0;
1490 int64_t OffsetFPR = 0;
1491 int64_t OffsetVR = 0;
1492 const int64_t TopOfStack =
1493 MFFrame.getOffsetAdjustment() + MFFrame.getStackSize();
1494
1495 // Loop over the spilled registers. The CalleeSavedInfo can't be used because
1496 // it does not contain all spilled registers.
1497 for (unsigned I = ZFI->getSpillGPRRegs().LowGPR,
1498 E = ZFI->getSpillGPRRegs().HighGPR;
1499 I && E && I <= E; ++I) {
1500 unsigned V = TRI->getEncodingValue((Register)I);
1501 assert(V < 16 && "GPR index out of range");
1502 SavedGPRMask |= 1 << (15 - V);
1503 }
1504
1505 for (auto &CS : CSI) {
1506 unsigned Reg = CS.getReg();
1507 unsigned I = TRI->getEncodingValue(Reg);
1508
1509 if (SystemZ::FP64BitRegClass.contains(Reg)) {
1510 assert(I < 16 && "FPR index out of range");
1511 SavedFPRMask |= 1 << (15 - I);
1512 int64_t Temp = MFFrame.getObjectOffset(CS.getFrameIdx());
1513 if (Temp < OffsetFPR)
1514 OffsetFPR = Temp;
1515 } else if (SystemZ::VR128BitRegClass.contains(Reg)) {
1516 assert(I >= 16 && I <= 23 && "VPR index out of range");
1517 unsigned BitNum = I - 16;
1518 SavedVRMask |= 1 << (7 - BitNum);
1519 int64_t Temp = MFFrame.getObjectOffset(CS.getFrameIdx());
1520 if (Temp < OffsetVR)
1521 OffsetVR = Temp;
1522 }
1523 }
1524
1525 // Adjust the offset.
1526 OffsetFPR += (OffsetFPR < 0) ? TopOfStack : 0;
1527 OffsetVR += (OffsetVR < 0) ? TopOfStack : 0;
1528
1529 // Get alloca register.
1530 uint8_t FrameReg = TRI->getEncodingValue(TRI->getFrameRegister(*MF));
1531 uint8_t AllocaReg = ZFL->hasFP(*MF) ? FrameReg : 0;
1532 assert(AllocaReg < 16 && "Can't have alloca register larger than 15");
1533 (void)AllocaReg;
1534
1535 // Build FPR save area offset.
1536 uint32_t FrameAndFPROffset = 0;
1537 if (SavedFPRMask) {
1538 uint64_t FPRSaveAreaOffset = OffsetFPR;
1539 assert(FPRSaveAreaOffset < 0x10000000 && "Offset out of range");
1540
1541 FrameAndFPROffset = FPRSaveAreaOffset & 0x0FFFFFFF; // Lose top 4 bits.
1542 FrameAndFPROffset |= FrameReg << 28; // Put into top 4 bits.
1543 }
1544
1545 // Build VR save area offset.
1546 uint32_t FrameAndVROffset = 0;
1547 if (TargetHasVector && SavedVRMask) {
1548 uint64_t VRSaveAreaOffset = OffsetVR;
1549 assert(VRSaveAreaOffset < 0x10000000 && "Offset out of range");
1550
1551 FrameAndVROffset = VRSaveAreaOffset & 0x0FFFFFFF; // Lose top 4 bits.
1552 FrameAndVROffset |= FrameReg << 28; // Put into top 4 bits.
1553 }
1554
1555 // Emit PPA1 section.
1556 OutStreamer->AddComment("PPA1");
1557 OutStreamer->emitLabel(CurrentFnPPA1Sym);
1558 OutStreamer->AddComment("Version");
1559 OutStreamer->emitInt8(0x02); // Version.
1560 OutStreamer->AddComment("LE Signature X'CE'");
1561 OutStreamer->emitInt8(0xCE); // CEL signature.
1562 OutStreamer->AddComment("Saved GPR Mask");
1563 OutStreamer->emitInt16(SavedGPRMask);
1564 OutStreamer->AddComment("Offset to PPA2");
1565 OutStreamer->emitAbsoluteSymbolDiff(PPA2Sym, CurrentFnPPA1Sym, 4);
1566
1567 bool NeedEmitEHBlock = !MF->getLandingPads().empty();
1568
1569 // Optional Argument Area Length.
1570 // Note: This represents the length of the argument area that we reserve
1571 // in our stack for setting up arguments for calls to other
1572 // routines. If this optional field is not set, LE will reserve
1573 // 128 bytes for the argument area. This optional field is
1574 // created if greater than 128 bytes is required - to guarantee
1575 // the required space is reserved on stack extension in the new
1576 // extension. This optional field is also created if the
1577 // routine has alloca(). This may reduce stack space
1578 // if alloca() call causes a stack extension.
1579 bool HasArgAreaLength =
1580 (AllocaReg != 0) || (MFFrame.getMaxCallFrameSize() > 128);
1581
1582 bool HasName =
1583 MF->getFunction().hasName() && MF->getFunction().getName().size() > 0;
1584
1585 emitPPA1Flags(OutStreamer, MF->getFunction().isVarArg(),
1586 MFFrame.hasStackProtectorIndex(), SavedFPRMask != 0,
1587 TargetHasVector && SavedVRMask != 0, NeedEmitEHBlock,
1588 HasArgAreaLength, HasName);
1589
1590 OutStreamer->AddComment("Length/4 of Parms");
1591 OutStreamer->emitInt16(
1592 static_cast<uint16_t>(ZFI->getSizeOfFnParams() / 4)); // Parms/4.
1593 OutStreamer->AddComment("Length of Code");
1594 OutStreamer->emitAbsoluteSymbolDiff(FnEndSym, CurrentFnEPMarkerSym, 4);
1595
1596 if (HasArgAreaLength) {
1597 OutStreamer->AddComment("Argument Area Length");
1598 OutStreamer->emitInt32(MFFrame.getMaxCallFrameSize());
1599 }
1600
1601 // Emit saved FPR mask and offset to FPR save area (0x20 of flags 3).
1602 if (SavedFPRMask) {
1603 OutStreamer->AddComment("FPR mask");
1604 OutStreamer->emitInt16(SavedFPRMask);
1605 OutStreamer->AddComment("AR mask");
1606 OutStreamer->emitInt16(0); // AR Mask, unused currently.
1607 OutStreamer->AddComment("FPR Save Area Locator");
1608 OutStreamer->AddComment(Twine(" Bit 0-3: Register R")
1609 .concat(utostr(FrameAndFPROffset >> 28))
1610 .str());
1611 OutStreamer->AddComment(Twine(" Bit 4-31: Offset ")
1612 .concat(utostr(FrameAndFPROffset & 0x0FFFFFFF))
1613 .str());
1614 OutStreamer->emitInt32(FrameAndFPROffset); // Offset to FPR save area with
1615 // register to add value to
1616 // (alloca reg).
1617 }
1618
1619 // Emit saved VR mask to VR save area.
1620 if (TargetHasVector && SavedVRMask) {
1621 OutStreamer->AddComment("VR mask");
1622 OutStreamer->emitInt8(SavedVRMask);
1623 OutStreamer->emitInt8(0); // Reserved.
1624 OutStreamer->emitInt16(0); // Also reserved.
1625 OutStreamer->AddComment("VR Save Area Locator");
1626 OutStreamer->AddComment(Twine(" Bit 0-3: Register R")
1627 .concat(utostr(FrameAndVROffset >> 28))
1628 .str());
1629 OutStreamer->AddComment(Twine(" Bit 4-31: Offset ")
1630 .concat(utostr(FrameAndVROffset & 0x0FFFFFFF))
1631 .str());
1632 OutStreamer->emitInt32(FrameAndVROffset);
1633 }
1634
1635 // Emit C++ EH information block
1636 const Function *Per = nullptr;
1637 if (NeedEmitEHBlock) {
1638 Per = dyn_cast<Function>(
1639 MF->getFunction().getPersonalityFn()->stripPointerCasts());
1640 MCSymbol *PersonalityRoutine =
1641 Per ? MF->getTarget().getSymbol(Per) : nullptr;
1642 assert(PersonalityRoutine && "Missing personality routine");
1643
1644 OutStreamer->AddComment("Version");
1645 OutStreamer->emitInt32(1);
1646 OutStreamer->AddComment("Flags");
1647 OutStreamer->emitInt32(0); // LSDA field is a WAS offset
1648 OutStreamer->AddComment("Personality routine");
1649 OutStreamer->emitInt64(ADATable.insert(
1650 PersonalityRoutine, SystemZII::MO_ADA_INDIRECT_FUNC_DESC));
1651 OutStreamer->AddComment("LSDA location");
1652 MCSymbol *GCCEH = MF->getContext().getOrCreateSymbol(
1653 Twine("GCC_except_table") + Twine(MF->getFunctionNumber()));
1654 OutStreamer->emitInt64(
1655 ADATable.insert(GCCEH, SystemZII::MO_ADA_DATA_SYMBOL_ADDR));
1656 }
1657
1658 // Emit name length and name optional section (0x01 of flags 4)
1659 if (HasName)
1660 emitPPA1Name(OutStreamer, MF->getFunction().getName());
1661
1662 // Emit offset to entry point optional section (0x80 of flags 4).
1663 OutStreamer->emitAbsoluteSymbolDiff(CurrentFnEPMarkerSym, CurrentFnPPA1Sym,
1664 4);
1665}
1666
1668 if (TM.getTargetTriple().isOSzOS())
1669 emitPPA2(M);
1671}
1672
1673void SystemZAsmPrinter::emitPPA2(Module &M) {
1674 OutStreamer->pushSection();
1675 OutStreamer->switchSection(getObjFileLowering().getTextSection(),
1677 MCContext &OutContext = OutStreamer->getContext();
1678 // Make CELQSTRT symbol.
1679 const char *StartSymbolName = "CELQSTRT";
1680 MCSymbol *CELQSTRT = OutContext.getOrCreateSymbol(StartSymbolName);
1681 OutStreamer->emitSymbolAttribute(CELQSTRT, MCSA_OSLinkage);
1682 OutStreamer->emitSymbolAttribute(CELQSTRT, MCSA_Global);
1683
1684 // Create symbol and assign to class field for use in PPA1.
1685 PPA2Sym = OutContext.createTempSymbol("PPA2", false);
1686 MCSymbol *DateVersionSym = OutContext.createTempSymbol("DVS", false);
1687
1688 std::time_t Time = getTranslationTime(M);
1689 SmallString<14> CompilationTimeEBCDIC, CompilationTime;
1690 CompilationTime = formatv("{0:%Y%m%d%H%M%S}", llvm::sys::toUtcTime(Time));
1691
1692 uint32_t ProductVersion = getProductVersion(M),
1693 ProductRelease = getProductRelease(M),
1694 ProductPatch = getProductPatch(M);
1695
1696 SmallString<6> VersionEBCDIC, Version;
1697 Version = formatv("{0,0-2:d}{1,0-2:d}{2,0-2:d}", ProductVersion,
1698 ProductRelease, ProductPatch);
1699
1700 ConverterEBCDIC::convertToEBCDIC(CompilationTime, CompilationTimeEBCDIC);
1702
1703 enum class PPA2MemberId : uint8_t {
1704 // See z/OS Language Environment Vendor Interfaces v2r5, p.23, for
1705 // complete list. Only the C runtime is supported by this backend.
1706 LE_C_Runtime = 3,
1707 };
1708 enum class PPA2MemberSubId : uint8_t {
1709 // List of languages using the LE C runtime implementation.
1710 C = 0x00,
1711 CXX = 0x01,
1712 Swift = 0x03,
1713 Go = 0x60,
1714 LLVMBasedLang = 0xe7,
1715 };
1716 // PPA2 Flags
1717 enum class PPA2Flags : uint8_t {
1718 CompileForBinaryFloatingPoint = 0x80,
1719 CompiledWithXPLink = 0x01,
1720 CompiledUnitASCII = 0x04,
1721 HasServiceInfo = 0x20,
1722 };
1723
1724 PPA2MemberSubId MemberSubId = PPA2MemberSubId::LLVMBasedLang;
1725 if (auto *MD = M.getModuleFlag("zos_cu_language")) {
1726 StringRef Language = cast<MDString>(MD)->getString();
1727 MemberSubId = StringSwitch<PPA2MemberSubId>(Language)
1728 .Case("C", PPA2MemberSubId::C)
1729 .Case("C++", PPA2MemberSubId::CXX)
1730 .Case("Swift", PPA2MemberSubId::Swift)
1731 .Case("Go", PPA2MemberSubId::Go)
1732 .Default(PPA2MemberSubId::LLVMBasedLang);
1733 }
1734
1735 // Emit PPA2 section.
1736 OutStreamer->emitLabel(PPA2Sym);
1737 OutStreamer->emitInt8(static_cast<uint8_t>(PPA2MemberId::LE_C_Runtime));
1738 OutStreamer->emitInt8(static_cast<uint8_t>(MemberSubId));
1739 OutStreamer->emitInt8(0x22); // Member defined, c370_plist+c370_env
1740 OutStreamer->emitInt8(0x04); // Control level 4 (XPLink)
1741 OutStreamer->emitAbsoluteSymbolDiff(CELQSTRT, PPA2Sym, 4);
1742 OutStreamer->emitInt32(0x00000000);
1743 OutStreamer->emitAbsoluteSymbolDiff(DateVersionSym, PPA2Sym, 4);
1744 OutStreamer->emitInt32(
1745 0x00000000); // Offset to main entry point, always 0 (so says TR).
1746 uint8_t Flgs = static_cast<uint8_t>(PPA2Flags::CompileForBinaryFloatingPoint);
1747 Flgs |= static_cast<uint8_t>(PPA2Flags::CompiledWithXPLink);
1748
1749 bool IsASCII = true;
1750 if (auto *MD = M.getModuleFlag("zos_le_char_mode")) {
1751 const StringRef &CharMode = cast<MDString>(MD)->getString();
1752 if (CharMode == "ebcdic")
1753 IsASCII = false;
1754 else if (CharMode != "ascii")
1755 OutContext.reportError(
1756 {}, "Only ascii or ebcdic are allowed for zos_le_char_mode");
1757 }
1758 if (IsASCII)
1759 Flgs |= static_cast<uint8_t>(
1760 PPA2Flags::CompiledUnitASCII); // Setting bit for ASCII char. mode.
1761
1762 OutStreamer->emitInt8(Flgs);
1763 OutStreamer->emitInt8(0x00); // Reserved.
1764 // No MD5 signature before timestamp.
1765 // No FLOAT(AFP(VOLATILE)).
1766 // Remaining 5 flag bits reserved.
1767 OutStreamer->emitInt16(0x0000); // 16 Reserved flag bits.
1768
1769 // Emit date and version section.
1770 OutStreamer->emitLabel(DateVersionSym);
1771 OutStreamer->emitBytes(CompilationTimeEBCDIC.str());
1772 OutStreamer->emitBytes(VersionEBCDIC.str());
1773
1774 OutStreamer->emitInt16(0x0000); // Service level string length.
1775
1776 // The binder requires that the offset to the PPA2 be emitted in a different,
1777 // specially-named section.
1778 OutStreamer->switchSection(getObjFileLowering().getPPA2ListSection());
1779 // Emit 8 byte alignment.
1780 // Emit pointer to PPA2 label.
1781 OutStreamer->AddComment("A(PPA2-CELQSTRT)");
1782 OutStreamer->emitAbsoluteSymbolDiff(PPA2Sym, CELQSTRT, 8);
1783 OutStreamer->popSection();
1784}
1785
1787 const SystemZSubtarget &Subtarget = MF->getSubtarget<SystemZSubtarget>();
1788
1789 if (Subtarget.getTargetTriple().isOSzOS()) {
1790 MCContext &OutContext = OutStreamer->getContext();
1791
1792 // Save information for later use.
1793 std::string N(MF->getFunction().hasName()
1794 ? Twine(MF->getFunction().getName()).concat("_").str()
1795 : "");
1796
1797 CurrentFnEPMarkerSym =
1798 OutContext.createTempSymbol(Twine("EPM_").concat(N).str(), true);
1799 CurrentFnPPA1Sym =
1800 OutContext.createTempSymbol(Twine("PPA1_").concat(N).str(), true);
1801
1802 // EntryPoint Marker
1803 const MachineFrameInfo &MFFrame = MF->getFrameInfo();
1804 bool IsUsingAlloca = MFFrame.hasVarSizedObjects();
1805 uint32_t DSASize = MFFrame.getStackSize();
1806 bool IsLeaf = DSASize == 0 && MFFrame.getCalleeSavedInfo().empty();
1807
1808 // Set Flags.
1809 uint8_t Flags = 0;
1810 if (IsLeaf)
1811 Flags |= 0x08;
1812 if (IsUsingAlloca)
1813 Flags |= 0x04;
1814
1815 // Combine into top 27 bits of DSASize and bottom 5 bits of Flags.
1816 uint32_t DSAAndFlags = DSASize & 0xFFFFFFE0; // (x/32) << 5
1817 DSAAndFlags |= Flags;
1818
1819 // Emit entry point marker section.
1820 OutStreamer->AddComment("XPLINK Routine Layout Entry");
1821 OutStreamer->emitLabel(CurrentFnEPMarkerSym);
1822 OutStreamer->AddComment("Eyecatcher 0x00C300C500C500");
1823 OutStreamer->emitIntValueInHex(0x00C300C500C500, 7); // Eyecatcher.
1824 OutStreamer->AddComment("Mark Type C'1'");
1825 OutStreamer->emitInt8(0xF1); // Mark Type.
1826 OutStreamer->AddComment("Offset to PPA1");
1827 OutStreamer->emitAbsoluteSymbolDiff(CurrentFnPPA1Sym, CurrentFnEPMarkerSym,
1828 4);
1829 if (OutStreamer->isVerboseAsm()) {
1830 OutStreamer->AddComment("DSA Size 0x" + Twine::utohexstr(DSASize));
1831 OutStreamer->AddComment("Entry Flags");
1832 if (Flags & 0x08)
1833 OutStreamer->AddComment(" Bit 1: 1 = Leaf function");
1834 else
1835 OutStreamer->AddComment(" Bit 1: 0 = Non-leaf function");
1836 if (Flags & 0x04)
1837 OutStreamer->AddComment(" Bit 2: 1 = Uses alloca");
1838 else
1839 OutStreamer->AddComment(" Bit 2: 0 = Does not use alloca");
1840 }
1841 OutStreamer->emitInt32(DSAAndFlags);
1842 }
1843
1845}
1846
1847char SystemZAsmPrinter::ID = 0;
1848
1849INITIALIZE_PASS(SystemZAsmPrinter, "systemz-asm-printer",
1850 "SystemZ Assembly Printer", false, false)
1851
1852// Force static initialization.
1853extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void
1854LLVMInitializeSystemZAsmPrinter() {
1856}
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static const Function * getParent(const Value *V)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ABI
Definition Compiler.h:213
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
This file provides utility functions for converting between EBCDIC-1047 and UTF-8.
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
#define RegName(no)
This file contains the MCSymbolGOFF class.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Machine Check Debug Module
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:487
static bool printOperand(raw_ostream &OS, const SelectionDAG *G, const SDValue Value)
This file contains some functions that are useful when dealing with strings.
static MCInst lowerVecEltExtraction(const MachineInstr *MI, unsigned Opcode)
static uint8_t getSpecifierFromModifier(SystemZCP::SystemZCPModifier Modifier)
static void emitPPA1Name(std::unique_ptr< MCStreamer > &OutStreamer, StringRef OutName)
#define LOWER_LOW(NAME)
static void lowerAlignmentHint(const MachineInstr *MI, MCInst &LoweredMI, unsigned Opcode)
#define EMIT_COMMENT(Str)
static const MCSymbolRefExpr * getGlobalOffsetTable(MCContext &Context)
#define LOWER_HIGH(NAME)
static void printFormattedRegName(const MCAsmInfo *MAI, unsigned RegNo, raw_ostream &OS)
static MCInst lowerRILow(const MachineInstr *MI, unsigned Opcode)
static uint32_t getProductVersion(Module &M)
static std::string getProductID(Module &M)
static MCInst lowerRIHigh(const MachineInstr *MI, unsigned Opcode)
static void printAddress(const MCAsmInfo *MAI, unsigned Base, const MCOperand &DispMO, unsigned Index, raw_ostream &OS)
static time_t getTranslationTime(Module &M)
static const MCSymbolRefExpr * getTLSGetOffset(MCContext &Context)
static void emitPPA1Flags(std::unique_ptr< MCStreamer > &OutStreamer, bool VarArg, bool StackProtector, bool FPRMask, bool VRMask, bool EHBlock, bool HasArgAreaLength, bool HasName)
static MCInst lowerSubvectorStore(const MachineInstr *MI, unsigned Opcode)
static unsigned EmitNop(MCContext &OutContext, MCStreamer &OutStreamer, unsigned NumBytes, const MCSubtargetInfo &STI)
static MCInst lowerVecEltInsertion(const MachineInstr *MI, unsigned Opcode)
static uint32_t getProductRelease(Module &M)
static MCInst lowerSubvectorLoad(const MachineInstr *MI, unsigned Opcode)
static uint32_t getProductPatch(Module &M)
static MCInst lowerRIEfLow(const MachineInstr *MI, unsigned Opcode)
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
MCSymbol * getSymbol(const GlobalValue *GV) const
void EmitToStreamer(MCStreamer &S, const MCInst &Inst)
TargetMachine & TM
Target machine description.
Definition AsmPrinter.h:94
const MCAsmInfo * MAI
Target Asm Printer information.
Definition AsmPrinter.h:97
MachineFunction * MF
The current machine function.
Definition AsmPrinter.h:109
virtual void emitStartOfAsmFile(Module &)
This virtual method can be overridden by targets that want to emit something at the start of their fi...
Definition AsmPrinter.h:595
void recordSled(MCSymbol *Sled, const MachineInstr &MI, SledKind Kind, uint8_t Version=0)
void emitAlignment(Align Alignment, const GlobalObject *GV=nullptr, unsigned MaxBytesToEmit=0) const
Emit an alignment directive to the specified power of two boundary.
MCContext & OutContext
This is the context for the output file that we are streaming.
Definition AsmPrinter.h:101
MCSymbol * createTempSymbol(const Twine &Name) const
virtual void emitXXStructorList(const DataLayout &DL, const Constant *List, bool IsCtor)
This method emits llvm.global_ctors or llvm.global_dtors list.
void emitInt32(int Value) const
Emit a long directive and value.
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition AsmPrinter.h:106
void preprocessXXStructorList(const DataLayout &DL, const Constant *List, SmallVector< Structor, 8 > &Structors)
This method gathers an array of Structors and then sorts them out by Priority.
const DataLayout & getDataLayout() const
Return information about data layout.
virtual void emitFunctionEntryLabel()
EmitFunctionEntryLabel - Emit the label that is the entrypoint for the function.
const MCSubtargetInfo & getSubtargetInfo() const
Return information about subtarget.
virtual bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS)
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant.
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
LLVM_ABI TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
LLVM_ABI unsigned getPointerSize(unsigned AS=0) const
The pointer representation size in bytes, rounded up to a whole number of bytes.
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition MCAsmInfo.h:64
unsigned getAssemblerDialect() const
Definition MCAsmInfo.h:563
void printExpr(raw_ostream &, const MCExpr &) const
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:343
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
Context object for machine code objects.
Definition MCContext.h:83
LLVM_ABI MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition MCContext.h:553
LLVM_ABI MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:34
MCInstBuilder & addReg(MCRegister Reg)
Add a new register operand.
MCInstBuilder & addImm(int64_t Val)
Add a new integer immediate operand.
MCInstBuilder & addExpr(const MCExpr *Val)
Add a new MCExpr operand.
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
void addOperand(const MCOperand Op)
Definition MCInst.h:215
void setOpcode(unsigned Op)
Definition MCInst.h:201
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:40
int64_t getImm() const
Definition MCInst.h:84
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:138
static MCOperand createImm(int64_t Val)
Definition MCInst.h:145
bool isImm() const
Definition MCInst.h:66
bool isReg() const
Definition MCInst.h:65
MCRegister getReg() const
Returns the register number.
Definition MCInst.h:73
const MCExpr * getExpr() const
Definition MCInst.h:118
bool isExpr() const
Definition MCInst.h:69
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
MCSymbol * getBeginSymbol()
Definition MCSection.h:590
static const MCSpecifierExpr * create(const MCExpr *Expr, Spec S, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.cpp:743
Streaming machine code generation interface.
Definition MCStreamer.h:220
Generic base class for all target subtargets.
Represent a reference to a symbol from inside an expression.
Definition MCExpr.h:190
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:214
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
StringRef getName() const
getName - Get the symbol name.
Definition MCSymbol.h:188
MachineInstrBundleIterator< const MachineInstr > const_iterator
Abstract base class for all machine specific constantpool value subclasses.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
uint64_t getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call.
int64_t getOffsetAdjustment() const
Return the correction for frame offsets.
const std::vector< CalleeSavedInfo > & getCalleeSavedInfo() const
Returns a reference to call saved info vector for the current function.
bool hasStackProtectorIndex() const
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
MCContext & getContext() const
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
Representation of each machine instruction.
ArrayRef< MachineMemOperand * >::iterator mmo_iterator
LLVM_ABI const MachineFunction * getMF() const
Return the function that contains the basic block that this instruction belongs to.
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
unsigned getTargetFlags() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
const char * getSymbolName() const
Register getReg() const
getReg - Returns the register number.
@ MO_GlobalAddress
Address of a global value.
@ MO_ExternalSymbol
Name of external global symbol.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Wrapper class representing virtual and physical registers.
Definition Register.h:20
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
StringRef str() const
Explicit conversion to StringRef.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:573
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:146
void emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) override
void emitFunctionBodyEnd() override
Targets can override this to emit stuff after the last basic block in the function.
void emitStartOfAsmFile(Module &M) override
This virtual method can be overridden by targets that want to emit something at the start of their fi...
void emitInstruction(const MachineInstr *MI) override
Targets should implement this to emit instructions.
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS) override
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant as...
void emitFunctionEntryLabel() override
EmitFunctionEntryLabel - Emit the label that is the entrypoint for the function.
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, const char *ExtraCode, raw_ostream &OS) override
Print the specified operand of MI, an INLINEASM instruction, using the specified assembler variant.
void emitXXStructorList(const DataLayout &DL, const Constant *List, bool IsCtor) override
This method emits llvm.global_ctors or llvm.global_dtors list.
void emitEndOfAsmFile(Module &M) override
This virtual method can be overridden by targets that want to emit something at the end of their file...
A SystemZ-specific constant pool value.
static const char * getRegisterName(MCRegister Reg)
static const char * getRegisterName(MCRegister Reg)
const SystemZInstrInfo * getInstrInfo() const override
const TargetFrameLowering * getFrameLowering() const override
std::pair< MCInst, const MCSubtargetInfo * > MCInstSTIPair
MCSymbol * getSymbol(const GlobalValue *GV) const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM_ABI std::string str() const
Return the twine contents as a std::string.
Definition Twine.cpp:17
Twine concat(const Twine &Suffix) const
Definition Twine.h:497
static Twine utohexstr(uint64_t Val)
Definition Twine.h:385
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Language[]
Key for Kernel::Metadata::mLanguage.
@ Swift
Calling convention for Swift.
Definition CallingConv.h:69
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI std::error_code convertToEBCDIC(StringRef Source, SmallVectorImpl< char > &Result)
@ SHF_ALLOC
Definition ELF.h:1248
@ SHT_PROGBITS
Definition ELF.h:1147
@ SK_PPA1
Definition GOFF.h:192
@ SK_PPA2
Definition GOFF.h:193
unsigned getRegAsGR32(unsigned Reg)
const unsigned GR64Regs[16]
unsigned getRegAsGRH32(unsigned Reg)
unsigned getRegAsVR128(unsigned Reg)
unsigned getRegAsGR64(unsigned Reg)
constexpr size_t NameSize
Definition XCOFF.h:30
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract_or_null(Y &&MD)
Extract a Value from Metadata, allowing null.
Definition Metadata.h:683
UtcTime< std::chrono::seconds > toUtcTime(std::time_t T)
Convert a std::time_t to a UtcTime.
Definition Chrono.h:44
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
Target & getTheSystemZTarget()
@ Offset
Definition DWP.cpp:532
@ Length
Definition DWP.cpp:532
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
std::string utostr(uint64_t X, bool isNeg=false)
detail::concat_range< ValueT, RangeTs... > concat(RangeTs &&...Ranges)
Returns a concatenated range across two or more ranges.
Definition STLExtras.h:1150
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
FunctionAddr VTableAddr uintptr_t uintptr_t Version
Definition InstrProf.h:302
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
@ MCSA_OSLinkage
symbol uses OS linkage (GOFF)
@ MCSA_IndirectSymbol
.indirect_symbol (MachO)
@ MCSA_Global
.type _foo, @gnu_unique_object
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
llvm.global_ctors and llvm.global_dtors are arrays of Structor structs.
Definition AsmPrinter.h:526
RegisterAsmPrinter - Helper template for registering a target specific assembly printer,...