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::VLR16:
545 case SystemZ::VLR32:
546 case SystemZ::VLR64:
547 LoweredMI = MCInstBuilder(SystemZ::VLR)
548 .addReg(SystemZMC::getRegAsVR128(MI->getOperand(0).getReg()))
549 .addReg(SystemZMC::getRegAsVR128(MI->getOperand(1).getReg()));
550 break;
551
552 case SystemZ::VL:
553 Lower.lower(MI, LoweredMI);
554 lowerAlignmentHint(MI, LoweredMI, SystemZ::VLAlign);
555 break;
556
557 case SystemZ::VST:
558 Lower.lower(MI, LoweredMI);
559 lowerAlignmentHint(MI, LoweredMI, SystemZ::VSTAlign);
560 break;
561
562 case SystemZ::VLM:
563 Lower.lower(MI, LoweredMI);
564 lowerAlignmentHint(MI, LoweredMI, SystemZ::VLMAlign);
565 break;
566
567 case SystemZ::VSTM:
568 Lower.lower(MI, LoweredMI);
569 lowerAlignmentHint(MI, LoweredMI, SystemZ::VSTMAlign);
570 break;
571
572 case SystemZ::VL16:
573 LoweredMI = lowerSubvectorLoad(MI, SystemZ::VLREPH);
574 break;
575
576 case SystemZ::VL32:
577 LoweredMI = lowerSubvectorLoad(MI, SystemZ::VLREPF);
578 break;
579
580 case SystemZ::VL64:
581 LoweredMI = lowerSubvectorLoad(MI, SystemZ::VLREPG);
582 break;
583
584 case SystemZ::VST16:
585 LoweredMI = lowerSubvectorStore(MI, SystemZ::VSTEH);
586 break;
587
588 case SystemZ::VST32:
589 LoweredMI = lowerSubvectorStore(MI, SystemZ::VSTEF);
590 break;
591
592 case SystemZ::VST64:
593 LoweredMI = lowerSubvectorStore(MI, SystemZ::VSTEG);
594 break;
595
596 case SystemZ::LFER:
597 LoweredMI = lowerVecEltExtraction(MI, SystemZ::VLGVF);
598 break;
599
600 case SystemZ::LFER_16:
601 LoweredMI = lowerVecEltExtraction(MI, SystemZ::VLGVH);
602 break;
603
604 case SystemZ::LEFR:
605 LoweredMI = lowerVecEltInsertion(MI, SystemZ::VLVGF);
606 break;
607
608 case SystemZ::LEFR_16:
609 LoweredMI = lowerVecEltInsertion(MI, SystemZ::VLVGH);
610 break;
611
612#define LOWER_LOW(NAME) \
613 case SystemZ::NAME##64: LoweredMI = lowerRILow(MI, SystemZ::NAME); break
614
615 LOWER_LOW(IILL);
616 LOWER_LOW(IILH);
617 LOWER_LOW(TMLL);
618 LOWER_LOW(TMLH);
619 LOWER_LOW(NILL);
620 LOWER_LOW(NILH);
621 LOWER_LOW(NILF);
622 LOWER_LOW(OILL);
623 LOWER_LOW(OILH);
624 LOWER_LOW(OILF);
625 LOWER_LOW(XILF);
626
627#undef LOWER_LOW
628
629#define LOWER_HIGH(NAME) \
630 case SystemZ::NAME##64: LoweredMI = lowerRIHigh(MI, SystemZ::NAME); break
631
632 LOWER_HIGH(IIHL);
633 LOWER_HIGH(IIHH);
634 LOWER_HIGH(TMHL);
635 LOWER_HIGH(TMHH);
636 LOWER_HIGH(NIHL);
637 LOWER_HIGH(NIHH);
638 LOWER_HIGH(NIHF);
639 LOWER_HIGH(OIHL);
640 LOWER_HIGH(OIHH);
641 LOWER_HIGH(OIHF);
642 LOWER_HIGH(XIHF);
643
644#undef LOWER_HIGH
645
646 case SystemZ::Serialize:
647 if (MF->getSubtarget<SystemZSubtarget>().hasFastSerialization())
648 LoweredMI = MCInstBuilder(SystemZ::BCRAsm)
649 .addImm(14).addReg(SystemZ::R0D);
650 else
651 LoweredMI = MCInstBuilder(SystemZ::BCRAsm)
652 .addImm(15).addReg(SystemZ::R0D);
653 break;
654
655 // We want to emit "j .+2" for traps, jumping to the relative immediate field
656 // of the jump instruction, which is an illegal instruction. We cannot emit a
657 // "." symbol, so create and emit a temp label before the instruction and use
658 // that instead.
659 case SystemZ::Trap: {
660 MCSymbol *DotSym = OutContext.createTempSymbol();
661 OutStreamer->emitLabel(DotSym);
662
664 const MCConstantExpr *ConstExpr = MCConstantExpr::create(2, OutContext);
665 LoweredMI = MCInstBuilder(SystemZ::J)
666 .addExpr(MCBinaryExpr::createAdd(Expr, ConstExpr, OutContext));
667 }
668 break;
669
670 // Conditional traps will create a branch on condition instruction that jumps
671 // to the relative immediate field of the jump instruction. (eg. "jo .+2")
672 case SystemZ::CondTrap: {
673 MCSymbol *DotSym = OutContext.createTempSymbol();
674 OutStreamer->emitLabel(DotSym);
675
677 const MCConstantExpr *ConstExpr = MCConstantExpr::create(2, OutContext);
678 LoweredMI = MCInstBuilder(SystemZ::BRC)
679 .addImm(MI->getOperand(0).getImm())
680 .addImm(MI->getOperand(1).getImm())
681 .addExpr(MCBinaryExpr::createAdd(Expr, ConstExpr, OutContext));
682 }
683 break;
684
685 case TargetOpcode::FENTRY_CALL:
686 LowerFENTRY_CALL(*MI, Lower);
687 return;
688
689 case TargetOpcode::STACKMAP:
690 LowerSTACKMAP(*MI);
691 return;
692
693 case TargetOpcode::PATCHPOINT:
694 LowerPATCHPOINT(*MI, Lower);
695 return;
696
697 case TargetOpcode::PATCHABLE_FUNCTION_ENTER:
698 LowerPATCHABLE_FUNCTION_ENTER(*MI, Lower);
699 return;
700
701 case TargetOpcode::PATCHABLE_RET:
702 LowerPATCHABLE_RET(*MI, Lower);
703 return;
704
705 case TargetOpcode::PATCHABLE_FUNCTION_EXIT:
706 llvm_unreachable("PATCHABLE_FUNCTION_EXIT should never be emitted");
707
708 case TargetOpcode::PATCHABLE_TAIL_CALL:
709 // TODO: Define a trampoline `__xray_FunctionTailExit` and differentiate a
710 // normal function exit from a tail exit.
711 llvm_unreachable("Tail call is handled in the normal case. See comments "
712 "around this assert.");
713
714 case SystemZ::EXRL_Pseudo: {
715 unsigned TargetInsOpc = MI->getOperand(0).getImm();
716 Register LenMinus1Reg = MI->getOperand(1).getReg();
717 Register DestReg = MI->getOperand(2).getReg();
718 int64_t DestDisp = MI->getOperand(3).getImm();
719 Register SrcReg = MI->getOperand(4).getReg();
720 int64_t SrcDisp = MI->getOperand(5).getImm();
721
722 SystemZTargetStreamer *TS = getTargetStreamer();
723 MCInst ET = MCInstBuilder(TargetInsOpc)
724 .addReg(DestReg)
725 .addImm(DestDisp)
726 .addImm(1)
727 .addReg(SrcReg)
728 .addImm(SrcDisp);
729 SystemZTargetStreamer::MCInstSTIPair ET_STI(ET, &MF->getSubtarget());
730 auto [It, Inserted] = TS->EXRLTargets2Sym.try_emplace(ET_STI);
731 if (Inserted)
732 It->second = OutContext.createTempSymbol();
733 MCSymbol *DotSym = It->second;
737 MCInstBuilder(SystemZ::EXRL).addReg(LenMinus1Reg).addExpr(Dot));
738 return;
739 }
740
741 // EH_SjLj_Setup is a dummy terminator instruction of size 0.
742 // It is used to handle the clobber register for builtin setjmp.
743 case SystemZ::EH_SjLj_Setup:
744 return;
745
746 default:
747 Lower.lower(MI, LoweredMI);
748 break;
749 }
750 EmitToStreamer(*OutStreamer, LoweredMI);
751}
752
753// Emit the largest nop instruction smaller than or equal to NumBytes
754// bytes. Return the size of nop emitted.
756 unsigned NumBytes, const MCSubtargetInfo &STI) {
757 if (NumBytes < 2) {
758 llvm_unreachable("Zero nops?");
759 return 0;
760 }
761 else if (NumBytes < 4) {
762 OutStreamer.emitInstruction(
763 MCInstBuilder(SystemZ::BCRAsm).addImm(0).addReg(SystemZ::R0D), STI);
764 return 2;
765 }
766 else if (NumBytes < 6) {
767 OutStreamer.emitInstruction(
768 MCInstBuilder(SystemZ::BCAsm).addImm(0).addReg(0).addImm(0).addReg(0),
769 STI);
770 return 4;
771 }
772 else {
775 OutStreamer.emitLabel(DotSym);
776 OutStreamer.emitInstruction(
777 MCInstBuilder(SystemZ::BRCLAsm).addImm(0).addExpr(Dot), STI);
778 return 6;
779 }
780}
781
782void SystemZAsmPrinter::LowerFENTRY_CALL(const MachineInstr &MI,
783 SystemZMCInstLower &Lower) {
784 MCContext &Ctx = MF->getContext();
785 if (MF->getFunction().hasFnAttribute("mrecord-mcount")) {
786 MCSymbol *DotSym = OutContext.createTempSymbol();
787 OutStreamer->pushSection();
788 OutStreamer->switchSection(
789 Ctx.getELFSection("__mcount_loc", ELF::SHT_PROGBITS, ELF::SHF_ALLOC));
790 OutStreamer->emitSymbolValue(DotSym, 8);
791 OutStreamer->popSection();
792 OutStreamer->emitLabel(DotSym);
793 }
794
795 if (MF->getFunction().hasFnAttribute("mnop-mcount")) {
797 return;
798 }
799
800 MCSymbol *fentry = Ctx.getOrCreateSymbol("__fentry__");
801 const MCSymbolRefExpr *Op =
803 OutStreamer->emitInstruction(
804 MCInstBuilder(SystemZ::BRASL).addReg(SystemZ::R0D).addExpr(Op),
806}
807
808void SystemZAsmPrinter::LowerSTACKMAP(const MachineInstr &MI) {
809 auto *TII = MF->getSubtarget<SystemZSubtarget>().getInstrInfo();
810
811 unsigned NumNOPBytes = MI.getOperand(1).getImm();
812
813 auto &Ctx = OutStreamer->getContext();
814 MCSymbol *MILabel = Ctx.createTempSymbol();
815 OutStreamer->emitLabel(MILabel);
816
817 SM.recordStackMap(*MILabel, MI);
818 assert(NumNOPBytes % 2 == 0 && "Invalid number of NOP bytes requested!");
819
820 // Scan ahead to trim the shadow.
821 unsigned ShadowBytes = 0;
822 const MachineBasicBlock &MBB = *MI.getParent();
824 ++MII;
825 while (ShadowBytes < NumNOPBytes) {
826 if (MII == MBB.end() ||
827 MII->getOpcode() == TargetOpcode::PATCHPOINT ||
828 MII->getOpcode() == TargetOpcode::STACKMAP)
829 break;
830 ShadowBytes += TII->getInstSizeInBytes(*MII);
831 if (MII->isCall())
832 break;
833 ++MII;
834 }
835
836 // Emit nops.
837 while (ShadowBytes < NumNOPBytes)
838 ShadowBytes += EmitNop(OutContext, *OutStreamer, NumNOPBytes - ShadowBytes,
840}
841
842// Lower a patchpoint of the form:
843// [<def>], <id>, <numBytes>, <target>, <numArgs>
844void SystemZAsmPrinter::LowerPATCHPOINT(const MachineInstr &MI,
845 SystemZMCInstLower &Lower) {
846 auto &Ctx = OutStreamer->getContext();
847 MCSymbol *MILabel = Ctx.createTempSymbol();
848 OutStreamer->emitLabel(MILabel);
849
850 SM.recordPatchPoint(*MILabel, MI);
851 PatchPointOpers Opers(&MI);
852
853 unsigned EncodedBytes = 0;
854 const MachineOperand &CalleeMO = Opers.getCallTarget();
855
856 if (CalleeMO.isImm()) {
857 uint64_t CallTarget = CalleeMO.getImm();
858 if (CallTarget) {
859 unsigned ScratchIdx = -1;
860 unsigned ScratchReg = 0;
861 do {
862 ScratchIdx = Opers.getNextScratchIdx(ScratchIdx + 1);
863 ScratchReg = MI.getOperand(ScratchIdx).getReg();
864 } while (ScratchReg == SystemZ::R0D);
865
866 // Materialize the call target address
867 EmitToStreamer(*OutStreamer, MCInstBuilder(SystemZ::LLILF)
868 .addReg(ScratchReg)
869 .addImm(CallTarget & 0xFFFFFFFF));
870 EncodedBytes += 6;
871 if (CallTarget >> 32) {
872 EmitToStreamer(*OutStreamer, MCInstBuilder(SystemZ::IIHF)
873 .addReg(ScratchReg)
874 .addImm(CallTarget >> 32));
875 EncodedBytes += 6;
876 }
877
878 EmitToStreamer(*OutStreamer, MCInstBuilder(SystemZ::BASR)
879 .addReg(SystemZ::R14D)
880 .addReg(ScratchReg));
881 EncodedBytes += 2;
882 }
883 } else if (CalleeMO.isGlobal()) {
884 const MCExpr *Expr = Lower.getExpr(CalleeMO, SystemZ::S_PLT);
885 EmitToStreamer(*OutStreamer, MCInstBuilder(SystemZ::BRASL)
886 .addReg(SystemZ::R14D)
887 .addExpr(Expr));
888 EncodedBytes += 6;
889 }
890
891 // Emit padding.
892 unsigned NumBytes = Opers.getNumPatchBytes();
893 assert(NumBytes >= EncodedBytes &&
894 "Patchpoint can't request size less than the length of a call.");
895 assert((NumBytes - EncodedBytes) % 2 == 0 &&
896 "Invalid number of NOP bytes requested!");
897 while (EncodedBytes < NumBytes)
898 EncodedBytes += EmitNop(OutContext, *OutStreamer, NumBytes - EncodedBytes,
900}
901
902void SystemZAsmPrinter::LowerPATCHABLE_FUNCTION_ENTER(
903 const MachineInstr &MI, SystemZMCInstLower &Lower) {
904
905 const MachineFunction &MF = *(MI.getParent()->getParent());
906 const Function &F = MF.getFunction();
907
908 // If patchable-function-entry is set, emit in-function nops here.
909 if (F.hasFnAttribute("patchable-function-entry")) {
910 unsigned Num;
911 // get M-N from function attribute (CodeGenFunction subtracts N
912 // from M to yield the correct patchable-function-entry).
913 if (F.getFnAttribute("patchable-function-entry")
914 .getValueAsString()
915 .getAsInteger(10, Num))
916 return;
917 // Emit M-N 2-byte nops. Use getNop() here instead of emitNops()
918 // to keep it aligned with the common code implementation emitting
919 // the prefix nops.
920 for (unsigned I = 0; I < Num; ++I)
921 EmitToStreamer(*OutStreamer, MF.getSubtarget().getInstrInfo()->getNop());
922 return;
923 }
924 // Otherwise, emit xray sled.
925 // .begin:
926 // j .end # -> stmg %r2, %r15, 16(%r15)
927 // nop
928 // llilf %2, FuncID
929 // brasl %r14, __xray_FunctionEntry@GOT
930 // .end:
931 //
932 // Update compiler-rt/lib/xray/xray_s390x.cpp accordingly when number
933 // of instructions change.
934 bool HasVectorFeature =
935 TM.getMCSubtargetInfo()->hasFeature(SystemZ::FeatureVector) &&
936 !TM.getMCSubtargetInfo()->hasFeature(SystemZ::FeatureSoftFloat);
937 MCSymbol *FuncEntry = OutContext.getOrCreateSymbol(
938 HasVectorFeature ? "__xray_FunctionEntryVec" : "__xray_FunctionEntry");
939 MCSymbol *BeginOfSled = OutContext.createTempSymbol("xray_sled_", true);
940 MCSymbol *EndOfSled = OutContext.createTempSymbol();
941 OutStreamer->emitLabel(BeginOfSled);
943 MCInstBuilder(SystemZ::J)
944 .addExpr(MCSymbolRefExpr::create(EndOfSled, OutContext)));
947 MCInstBuilder(SystemZ::LLILF).addReg(SystemZ::R2D).addImm(0));
948 EmitToStreamer(*OutStreamer, MCInstBuilder(SystemZ::BRASL)
949 .addReg(SystemZ::R14D)
951 FuncEntry, SystemZ::S_PLT, OutContext)));
952 OutStreamer->emitLabel(EndOfSled);
953 recordSled(BeginOfSled, MI, SledKind::FUNCTION_ENTER, 2);
954}
955
956void SystemZAsmPrinter::LowerPATCHABLE_RET(const MachineInstr &MI,
957 SystemZMCInstLower &Lower) {
958 unsigned OpCode = MI.getOperand(0).getImm();
959 MCSymbol *FallthroughLabel = nullptr;
960 if (OpCode == SystemZ::CondReturn) {
961 FallthroughLabel = OutContext.createTempSymbol();
962 int64_t Cond0 = MI.getOperand(1).getImm();
963 int64_t Cond1 = MI.getOperand(2).getImm();
964 EmitToStreamer(*OutStreamer, MCInstBuilder(SystemZ::BRC)
965 .addImm(Cond0)
966 .addImm(Cond1 ^ Cond0)
968 FallthroughLabel, OutContext)));
969 }
970 // .begin:
971 // br %r14 # -> stmg %r2, %r15, 24(%r15)
972 // nop
973 // nop
974 // llilf %2,FuncID
975 // j __xray_FunctionExit@GOT
976 //
977 // Update compiler-rt/lib/xray/xray_s390x.cpp accordingly when number
978 // of instructions change.
979 bool HasVectorFeature =
980 TM.getMCSubtargetInfo()->hasFeature(SystemZ::FeatureVector) &&
981 !TM.getMCSubtargetInfo()->hasFeature(SystemZ::FeatureSoftFloat);
982 MCSymbol *FuncExit = OutContext.getOrCreateSymbol(
983 HasVectorFeature ? "__xray_FunctionExitVec" : "__xray_FunctionExit");
984 MCSymbol *BeginOfSled = OutContext.createTempSymbol("xray_sled_", true);
985 OutStreamer->emitLabel(BeginOfSled);
987 MCInstBuilder(SystemZ::BR).addReg(SystemZ::R14D));
990 MCInstBuilder(SystemZ::LLILF).addReg(SystemZ::R2D).addImm(0));
991 EmitToStreamer(*OutStreamer, MCInstBuilder(SystemZ::J)
993 FuncExit, SystemZ::S_PLT, OutContext)));
994 if (FallthroughLabel)
995 OutStreamer->emitLabel(FallthroughLabel);
996 recordSled(BeginOfSled, MI, SledKind::FUNCTION_EXIT, 2);
997}
998
999// The *alignment* of 128-bit vector types is different between the software
1000// and hardware vector ABIs. If the there is an externally visible use of a
1001// vector type in the module it should be annotated with an attribute.
1002void SystemZAsmPrinter::emitAttributes(Module &M) {
1003 if (M.getModuleFlag("s390x-visible-vector-ABI")) {
1004 bool HasVectorFeature =
1005 TM.getMCSubtargetInfo()->hasFeature(SystemZ::FeatureVector);
1006 OutStreamer->emitGNUAttribute(8, HasVectorFeature ? 2 : 1);
1007 }
1008}
1009
1010// Convert a SystemZ-specific constant pool modifier into the associated
1011// specifier.
1013 switch (Modifier) {
1014 case SystemZCP::TLSGD:
1015 return SystemZ::S_TLSGD;
1016 case SystemZCP::TLSLDM:
1017 return SystemZ::S_TLSLDM;
1018 case SystemZCP::DTPOFF:
1019 return SystemZ::S_DTPOFF;
1020 case SystemZCP::NTPOFF:
1021 return SystemZ::S_NTPOFF;
1022 }
1023 llvm_unreachable("Invalid SystemCPModifier!");
1024}
1025
1028 auto *ZCPV = static_cast<SystemZConstantPoolValue*>(MCPV);
1029
1030 const MCExpr *Expr = MCSymbolRefExpr::create(
1031 getSymbol(ZCPV->getGlobalValue()),
1032 getSpecifierFromModifier(ZCPV->getModifier()), OutContext);
1033 uint64_t Size = getDataLayout().getTypeAllocSize(ZCPV->getType());
1034
1035 OutStreamer->emitValue(Expr, Size);
1036}
1037
1038// Emit the ctor or dtor list taking into account the init priority.
1040 const Constant *List, bool IsCtor) {
1041 if (!TM.getTargetTriple().isOSBinFormatGOFF())
1042 return AsmPrinter::emitXXStructorList(DL, List, IsCtor);
1043
1044 SmallVector<Structor, 8> Structors;
1045 preprocessXXStructorList(DL, List, Structors);
1046 if (Structors.empty())
1047 return;
1048
1049 const Align Align = llvm::Align(4);
1050 const TargetLoweringObjectFileGOFF &Obj =
1051 static_cast<const TargetLoweringObjectFileGOFF &>(getObjFileLowering());
1052 for (Structor &S : Structors) {
1053 MCSectionGOFF *Section =
1054 static_cast<MCSectionGOFF *>(Obj.getStaticXtorSection(S.Priority));
1055 OutStreamer->switchSection(Section);
1056 if (OutStreamer->getCurrentSection() != OutStreamer->getPreviousSection())
1058
1059 // The priority is provided as an input to getStaticXtorSection(), and is
1060 // recalculated within that function as `Prio` going to going into the
1061 // PR section.
1062 // This priority retrieved via the `SortKey` below is the recalculated
1063 // Priority.
1064 uint32_t XtorPriority = Section->getPRAttributes().SortKey;
1065
1066 const GlobalValue *GV = dyn_cast<GlobalValue>(S.Func->stripPointerCasts());
1067 assert(GV && "C++ xxtor pointer was not a GlobalValue!");
1068 MCSymbolGOFF *Symbol = static_cast<MCSymbolGOFF *>(getSymbol(GV));
1069
1070 // @@SQINIT entry: { unsigned prio; void (*ctor)(); void (*dtor)(); }
1071
1072 unsigned PointerSizeInBytes = DL.getPointerSize();
1073
1074 auto &Ctx = OutStreamer->getContext();
1075 const MCExpr *ADAFuncRefExpr;
1076 unsigned SlotKind = SystemZII::MO_ADA_DIRECT_FUNC_DESC;
1077
1078 MCSectionGOFF *ADASection =
1079 static_cast<MCSectionGOFF *>(Obj.getADASection());
1080 assert(ADASection && "ADA section must exist for GOFF targets!");
1081 const MCSymbol *ADASym = ADASection->getBeginSymbol();
1082 assert(ADASym && "ADA symbol should already be set!");
1083
1084 ADAFuncRefExpr = MCBinaryExpr::createAdd(
1087 MCConstantExpr::create(ADATable.insert(Symbol, SlotKind), Ctx), Ctx);
1088
1089 emitInt32(XtorPriority);
1090 if (IsCtor) {
1091 OutStreamer->emitValue(ADAFuncRefExpr, PointerSizeInBytes);
1092 OutStreamer->emitIntValue(0, PointerSizeInBytes);
1093 } else {
1094 OutStreamer->emitIntValue(0, PointerSizeInBytes);
1095 OutStreamer->emitValue(ADAFuncRefExpr, PointerSizeInBytes);
1096 }
1097 }
1098}
1099
1100static void printFormattedRegName(const MCAsmInfo *MAI, unsigned RegNo,
1101 raw_ostream &OS) {
1102 const char *RegName;
1103 if (MAI->getAssemblerDialect() == AD_HLASM) {
1105 // Skip register prefix so that only register number is left
1106 assert(isalpha(RegName[0]) && isdigit(RegName[1]));
1107 OS << (RegName + 1);
1108 } else {
1110 OS << '%' << RegName;
1111 }
1112}
1113
1114static void printReg(unsigned Reg, const MCAsmInfo *MAI, raw_ostream &OS) {
1115 if (!Reg)
1116 OS << '0';
1117 else
1119}
1120
1121static void printOperand(const MCOperand &MCOp, const MCAsmInfo *MAI,
1122 raw_ostream &OS) {
1123 if (MCOp.isReg())
1124 printReg(MCOp.getReg(), MAI, OS);
1125 else if (MCOp.isImm())
1126 OS << MCOp.getImm();
1127 else if (MCOp.isExpr())
1128 MAI->printExpr(OS, *MCOp.getExpr());
1129 else
1130 llvm_unreachable("Invalid operand");
1131}
1132
1133static void printAddress(const MCAsmInfo *MAI, unsigned Base,
1134 const MCOperand &DispMO, unsigned Index,
1135 raw_ostream &OS) {
1136 printOperand(DispMO, MAI, OS);
1137 if (Base || Index) {
1138 OS << '(';
1139 if (Index) {
1140 printFormattedRegName(MAI, Index, OS);
1141 if (Base)
1142 OS << ',';
1143 }
1144 if (Base)
1146 OS << ')';
1147 }
1148}
1149
1151 const char *ExtraCode,
1152 raw_ostream &OS) {
1153 const MCRegisterInfo &MRI = *TM.getMCRegisterInfo();
1154 const MachineOperand &MO = MI->getOperand(OpNo);
1155 MCOperand MCOp;
1156 if (ExtraCode) {
1157 if (ExtraCode[0] == 'N' && !ExtraCode[1] && MO.isReg() &&
1158 SystemZ::GR128BitRegClass.contains(MO.getReg()))
1159 MCOp =
1160 MCOperand::createReg(MRI.getSubReg(MO.getReg(), SystemZ::subreg_l64));
1161 else
1162 return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS);
1163 } else {
1164 SystemZMCInstLower Lower(MF->getContext(), *this);
1165 MCOp = Lower.lowerOperand(MO);
1166 }
1167 printOperand(MCOp, MAI, OS);
1168 return false;
1169}
1170
1172 unsigned OpNo,
1173 const char *ExtraCode,
1174 raw_ostream &OS) {
1175 if (ExtraCode && ExtraCode[0] && !ExtraCode[1]) {
1176 switch (ExtraCode[0]) {
1177 case 'A':
1178 // Unlike EmitMachineNode(), EmitSpecialNode(INLINEASM) does not call
1179 // setMemRefs(), so MI->memoperands() is empty and the alignment
1180 // information is not available.
1181 return false;
1182 case 'O':
1183 OS << MI->getOperand(OpNo + 1).getImm();
1184 return false;
1185 case 'R':
1186 ::printReg(MI->getOperand(OpNo).getReg(), MAI, OS);
1187 return false;
1188 }
1189 }
1190 printAddress(MAI, MI->getOperand(OpNo).getReg(),
1191 MCOperand::createImm(MI->getOperand(OpNo + 1).getImm()),
1192 MI->getOperand(OpNo + 2).getReg(), OS);
1193 return false;
1194}
1195
1197 auto TT = OutContext.getTargetTriple();
1198 if (TT.isOSzOS()) {
1199 emitADASection();
1200 emitIDRLSection(M);
1201 }
1202 emitAttributes(M);
1203}
1204
1205void SystemZAsmPrinter::emitADASection() {
1206 OutStreamer->pushSection();
1207
1208 const unsigned PointerSize = getDataLayout().getPointerSize();
1209 OutStreamer->switchSection(getObjFileLowering().getADASection());
1210
1211 unsigned EmittedBytes = 0;
1212 for (auto &Entry : ADATable.getTable()) {
1213 const MCSymbol *Sym;
1214 unsigned SlotKind;
1215 std::tie(Sym, SlotKind) = Entry.first;
1216 unsigned Offset = Entry.second;
1217 assert(Offset == EmittedBytes && "Offset not as expected");
1218 (void)EmittedBytes;
1219#define EMIT_COMMENT(Str) \
1220 OutStreamer->AddComment(Twine("Offset ") \
1221 .concat(utostr(Offset)) \
1222 .concat(" " Str " ") \
1223 .concat(Sym->getName()));
1224 switch (SlotKind) {
1226 // Language Environment DLL logic requires function descriptors, for
1227 // imported functions, that are placed in the ADA to be 8 byte aligned.
1228 EMIT_COMMENT("function descriptor of");
1229 OutStreamer->emitValue(
1232 PointerSize);
1233 OutStreamer->emitValue(
1236 PointerSize);
1237 EmittedBytes += PointerSize * 2;
1238 break;
1240 EMIT_COMMENT("pointer to data symbol");
1241 OutStreamer->emitValue(
1244 PointerSize);
1245 EmittedBytes += PointerSize;
1246 break;
1249 Twine(Sym->getName()).concat("@indirect"));
1250 OutStreamer->emitAssignment(Alias,
1252 OutStreamer->emitSymbolAttribute(Alias, MCSA_IndirectSymbol);
1253
1254 EMIT_COMMENT("pointer to function descriptor");
1255 OutStreamer->emitValue(
1258 PointerSize);
1259 EmittedBytes += PointerSize;
1260 break;
1261 }
1262 default:
1263 llvm_unreachable("Unexpected slot kind");
1264 }
1265#undef EMIT_COMMENT
1266 }
1267 OutStreamer->popSection();
1268}
1269
1270static std::string getProductID(Module &M) {
1271 std::string ProductID;
1272 if (auto *MD = M.getModuleFlag("zos_product_id"))
1273 ProductID = cast<MDString>(MD)->getString().str();
1274 if (ProductID.empty())
1275 ProductID = "LLVM";
1276 return ProductID;
1277}
1278
1280 if (auto *VersionVal = mdconst::extract_or_null<ConstantInt>(
1281 M.getModuleFlag("zos_product_major_version")))
1282 return VersionVal->getZExtValue();
1283 return LLVM_VERSION_MAJOR;
1284}
1285
1287 if (auto *ReleaseVal = mdconst::extract_or_null<ConstantInt>(
1288 M.getModuleFlag("zos_product_minor_version")))
1289 return ReleaseVal->getZExtValue();
1290 return LLVM_VERSION_MINOR;
1291}
1292
1294 if (auto *PatchVal = mdconst::extract_or_null<ConstantInt>(
1295 M.getModuleFlag("zos_product_patchlevel")))
1296 return PatchVal->getZExtValue();
1297 return LLVM_VERSION_PATCH;
1298}
1299
1300static time_t getTranslationTime(Module &M) {
1301 std::time_t Time = 0;
1303 M.getModuleFlag("zos_translation_time"))) {
1304 long SecondsSinceEpoch = Val->getSExtValue();
1305 Time = static_cast<time_t>(SecondsSinceEpoch);
1306 }
1307 return Time;
1308}
1309
1310void SystemZAsmPrinter::emitIDRLSection(Module &M) {
1311 OutStreamer->pushSection();
1312 OutStreamer->switchSection(getObjFileLowering().getIDRLSection());
1313 constexpr unsigned IDRLDataLength = 30;
1314 std::time_t Time = getTranslationTime(M);
1315
1316 uint32_t ProductVersion = getProductVersion(M);
1317 uint32_t ProductRelease = getProductRelease(M);
1318
1319 std::string ProductID = getProductID(M);
1320
1321 SmallString<IDRLDataLength + 1> TempStr;
1322 raw_svector_ostream O(TempStr);
1323 O << formatv("{0,-10}{1,0-2:d}{2,0-2:d}{3:%Y%m%d%H%M%S}{4,0-2}",
1324 ProductID.substr(0, 10).c_str(), ProductVersion, ProductRelease,
1325 llvm::sys::toUtcTime(Time), "0");
1326 SmallString<IDRLDataLength> Data;
1328
1329 OutStreamer->emitInt8(0); // Reserved.
1330 OutStreamer->emitInt8(3); // Format.
1331 OutStreamer->emitInt16(IDRLDataLength); // Length.
1332 OutStreamer->emitBytes(Data.str());
1333 OutStreamer->popSection();
1334}
1335
1337 if (TM.getTargetTriple().isOSzOS()) {
1338 // Emit symbol for the end of function if the z/OS target streamer
1339 // is used. This is needed to calculate the size of the function.
1340 MCSymbol *FnEndSym = createTempSymbol("func_end");
1341 OutStreamer->emitLabel(FnEndSym);
1342
1343 OutStreamer->pushSection();
1344 OutStreamer->switchSection(getObjFileLowering().getTextSection(),
1346 emitPPA1(FnEndSym);
1347 OutStreamer->popSection();
1348
1349 CurrentFnPPA1Sym = nullptr;
1350 CurrentFnEPMarkerSym = nullptr;
1351 }
1352}
1353
1354static void emitPPA1Flags(std::unique_ptr<MCStreamer> &OutStreamer, bool VarArg,
1355 bool StackProtector, bool FPRMask, bool VRMask,
1356 bool EHBlock, bool HasArgAreaLength, bool HasName) {
1357 enum class PPA1Flag1 : uint8_t {
1358 DSA64Bit = (0x80 >> 0),
1359 VarArg = (0x80 >> 7),
1361 };
1362 enum class PPA1Flag2 : uint8_t {
1363 ExternalProcedure = (0x80 >> 0),
1364 STACKPROTECTOR = (0x80 >> 3),
1365 LLVM_MARK_AS_BITMASK_ENUM(ExternalProcedure)
1366 };
1367 enum class PPA1Flag3 : uint8_t {
1368 HasArgAreaLength = (0x80 >> 1),
1369 FPRMask = (0x80 >> 2),
1370 LLVM_MARK_AS_BITMASK_ENUM(HasArgAreaLength)
1371 };
1372 enum class PPA1Flag4 : uint8_t {
1373 EPMOffsetPresent = (0x80 >> 0),
1374 VRMask = (0x80 >> 2),
1375 EHBlock = (0x80 >> 3),
1376 ProcedureNamePresent = (0x80 >> 7),
1377 LLVM_MARK_AS_BITMASK_ENUM(EPMOffsetPresent)
1378 };
1379
1380 // Declare optional section flags that can be modified.
1381 auto Flags1 = PPA1Flag1(0);
1382 auto Flags2 = PPA1Flag2::ExternalProcedure;
1383 auto Flags3 = PPA1Flag3(0);
1384 auto Flags4 = PPA1Flag4::EPMOffsetPresent;
1385
1386 Flags1 |= PPA1Flag1::DSA64Bit;
1387
1388 if (VarArg)
1389 Flags1 |= PPA1Flag1::VarArg;
1390
1391 if (StackProtector)
1392 Flags2 |= PPA1Flag2::STACKPROTECTOR;
1393
1394 if (HasArgAreaLength)
1395 Flags3 |= PPA1Flag3::HasArgAreaLength; // Add emit ArgAreaLength flag.
1396
1397 // SavedGPRMask, SavedFPRMask, and SavedVRMask are precomputed in.
1398 if (FPRMask)
1399 Flags3 |= PPA1Flag3::FPRMask; // Add emit FPR mask flag.
1400
1401 if (VRMask)
1402 Flags4 |= PPA1Flag4::VRMask; // Add emit VR mask flag.
1403
1404 if (EHBlock)
1405 Flags4 |= PPA1Flag4::EHBlock; // Add optional EH block.
1406
1407 if (HasName)
1408 Flags4 |= PPA1Flag4::ProcedureNamePresent; // Add optional name block.
1409
1410 OutStreamer->AddComment("PPA1 Flags 1");
1411 if ((Flags1 & PPA1Flag1::DSA64Bit) == PPA1Flag1::DSA64Bit)
1412 OutStreamer->AddComment(" Bit 0: 1 = 64-bit DSA");
1413 else
1414 OutStreamer->AddComment(" Bit 0: 0 = 32-bit DSA");
1415 if ((Flags1 & PPA1Flag1::VarArg) == PPA1Flag1::VarArg)
1416 OutStreamer->AddComment(" Bit 7: 1 = Vararg function");
1417 OutStreamer->emitInt8(static_cast<uint8_t>(Flags1)); // Flags 1.
1418
1419 OutStreamer->AddComment("PPA1 Flags 2");
1420 if ((Flags2 & PPA1Flag2::ExternalProcedure) == PPA1Flag2::ExternalProcedure)
1421 OutStreamer->AddComment(" Bit 0: 1 = External procedure");
1422 if ((Flags2 & PPA1Flag2::STACKPROTECTOR) == PPA1Flag2::STACKPROTECTOR)
1423 OutStreamer->AddComment(" Bit 3: 1 = STACKPROTECT is enabled");
1424 else
1425 OutStreamer->AddComment(" Bit 3: 0 = STACKPROTECT is not enabled");
1426 OutStreamer->emitInt8(static_cast<uint8_t>(Flags2)); // Flags 2.
1427
1428 OutStreamer->AddComment("PPA1 Flags 3");
1429 if ((Flags3 & PPA1Flag3::HasArgAreaLength) == PPA1Flag3::HasArgAreaLength)
1430 OutStreamer->AddComment(
1431 " Bit 1: 1 = Argument Area Length is in optional area");
1432 if ((Flags3 & PPA1Flag3::FPRMask) == PPA1Flag3::FPRMask)
1433 OutStreamer->AddComment(" Bit 2: 1 = FP Reg Mask is in optional area");
1434 OutStreamer->emitInt8(
1435 static_cast<uint8_t>(Flags3)); // Flags 3 (optional sections).
1436
1437 OutStreamer->AddComment("PPA1 Flags 4");
1438 if ((Flags4 & PPA1Flag4::VRMask) == PPA1Flag4::VRMask)
1439 OutStreamer->AddComment(" Bit 2: 1 = Vector Reg Mask is in optional area");
1440 if ((Flags4 & PPA1Flag4::EHBlock) == PPA1Flag4::EHBlock)
1441 OutStreamer->AddComment(" Bit 3: 1 = C++ EH block");
1442 if ((Flags4 & PPA1Flag4::ProcedureNamePresent) ==
1443 PPA1Flag4::ProcedureNamePresent)
1444 OutStreamer->AddComment(" Bit 7: 1 = Name Length and Name");
1445 OutStreamer->emitInt8(static_cast<uint8_t>(
1446 Flags4)); // Flags 4 (optional sections, always emit these).
1447}
1448
1449static void emitPPA1Name(std::unique_ptr<MCStreamer> &OutStreamer,
1450 StringRef OutName) {
1451 size_t NameSize = OutName.size();
1452 uint16_t OutSize;
1453 if (NameSize < UINT16_MAX) {
1454 OutSize = static_cast<uint16_t>(NameSize);
1455 } else {
1456 OutName = OutName.substr(0, UINT16_MAX);
1457 OutSize = UINT16_MAX;
1458 }
1459 // Emit padding to ensure that the next optional field word-aligned.
1460 uint8_t ExtraZeros = 4 - ((2 + OutSize) % 4);
1461
1462 SmallString<512> OutnameConv;
1463 ConverterEBCDIC::convertToEBCDIC(OutName, OutnameConv);
1464 OutName = OutnameConv.str();
1465
1466 OutStreamer->AddComment("Length of Name");
1467 OutStreamer->emitInt16(OutSize);
1468 OutStreamer->AddComment("Name of Function");
1469 OutStreamer->emitBytes(OutName);
1470 OutStreamer->emitZeros(ExtraZeros);
1471}
1472
1473void SystemZAsmPrinter::emitPPA1(MCSymbol *FnEndSym) {
1474 assert(PPA2Sym != nullptr && "PPA2 Symbol not defined");
1475
1476 const TargetRegisterInfo *TRI = MF->getRegInfo().getTargetRegisterInfo();
1477 const SystemZSubtarget &Subtarget = MF->getSubtarget<SystemZSubtarget>();
1478 const auto TargetHasVector = Subtarget.hasVector();
1479
1480 const SystemZMachineFunctionInfo *ZFI =
1481 MF->getInfo<SystemZMachineFunctionInfo>();
1482 const auto *ZFL = static_cast<const SystemZXPLINKFrameLowering *>(
1483 Subtarget.getFrameLowering());
1484 const MachineFrameInfo &MFFrame = MF->getFrameInfo();
1485
1486 // Get saved GPR/FPR/VPR masks.
1487 const std::vector<CalleeSavedInfo> &CSI = MFFrame.getCalleeSavedInfo();
1488 uint16_t SavedGPRMask = 0;
1489 uint16_t SavedFPRMask = 0;
1490 uint8_t SavedVRMask = 0;
1491 int64_t OffsetFPR = 0;
1492 int64_t OffsetVR = 0;
1493 const int64_t TopOfStack =
1494 MFFrame.getOffsetAdjustment() + MFFrame.getStackSize();
1495
1496 // Loop over the spilled registers. The CalleeSavedInfo can't be used because
1497 // it does not contain all spilled registers.
1498 for (unsigned I = ZFI->getSpillGPRRegs().LowGPR,
1499 E = ZFI->getSpillGPRRegs().HighGPR;
1500 I && E && I <= E; ++I) {
1501 unsigned V = TRI->getEncodingValue((Register)I);
1502 assert(V < 16 && "GPR index out of range");
1503 SavedGPRMask |= 1 << (15 - V);
1504 }
1505
1506 for (auto &CS : CSI) {
1507 unsigned Reg = CS.getReg();
1508 unsigned I = TRI->getEncodingValue(Reg);
1509
1510 if (SystemZ::FP64BitRegClass.contains(Reg)) {
1511 assert(I < 16 && "FPR index out of range");
1512 SavedFPRMask |= 1 << (15 - I);
1513 int64_t Temp = MFFrame.getObjectOffset(CS.getFrameIdx());
1514 if (Temp < OffsetFPR)
1515 OffsetFPR = Temp;
1516 } else if (SystemZ::VR128BitRegClass.contains(Reg)) {
1517 assert(I >= 16 && I <= 23 && "VPR index out of range");
1518 unsigned BitNum = I - 16;
1519 SavedVRMask |= 1 << (7 - BitNum);
1520 int64_t Temp = MFFrame.getObjectOffset(CS.getFrameIdx());
1521 if (Temp < OffsetVR)
1522 OffsetVR = Temp;
1523 }
1524 }
1525
1526 // Adjust the offset.
1527 OffsetFPR += (OffsetFPR < 0) ? TopOfStack : 0;
1528 OffsetVR += (OffsetVR < 0) ? TopOfStack : 0;
1529
1530 // Get alloca register.
1531 uint8_t FrameReg = TRI->getEncodingValue(TRI->getFrameRegister(*MF));
1532 uint8_t AllocaReg = ZFL->hasFP(*MF) ? FrameReg : 0;
1533 assert(AllocaReg < 16 && "Can't have alloca register larger than 15");
1534 (void)AllocaReg;
1535
1536 // Build FPR save area offset.
1537 uint32_t FrameAndFPROffset = 0;
1538 if (SavedFPRMask) {
1539 uint64_t FPRSaveAreaOffset = OffsetFPR;
1540 assert(FPRSaveAreaOffset < 0x10000000 && "Offset out of range");
1541
1542 FrameAndFPROffset = FPRSaveAreaOffset & 0x0FFFFFFF; // Lose top 4 bits.
1543 FrameAndFPROffset |= FrameReg << 28; // Put into top 4 bits.
1544 }
1545
1546 // Build VR save area offset.
1547 uint32_t FrameAndVROffset = 0;
1548 if (TargetHasVector && SavedVRMask) {
1549 uint64_t VRSaveAreaOffset = OffsetVR;
1550 assert(VRSaveAreaOffset < 0x10000000 && "Offset out of range");
1551
1552 FrameAndVROffset = VRSaveAreaOffset & 0x0FFFFFFF; // Lose top 4 bits.
1553 FrameAndVROffset |= FrameReg << 28; // Put into top 4 bits.
1554 }
1555
1556 // Emit PPA1 section.
1557 OutStreamer->AddComment("PPA1");
1558 OutStreamer->emitLabel(CurrentFnPPA1Sym);
1559 OutStreamer->AddComment("Version");
1560 OutStreamer->emitInt8(0x02); // Version.
1561 OutStreamer->AddComment("LE Signature X'CE'");
1562 OutStreamer->emitInt8(0xCE); // CEL signature.
1563 OutStreamer->AddComment("Saved GPR Mask");
1564 OutStreamer->emitInt16(SavedGPRMask);
1565 OutStreamer->AddComment("Offset to PPA2");
1566 OutStreamer->emitAbsoluteSymbolDiff(PPA2Sym, CurrentFnPPA1Sym, 4);
1567
1568 bool NeedEmitEHBlock = !MF->getLandingPads().empty();
1569
1570 // Optional Argument Area Length.
1571 // Note: This represents the length of the argument area that we reserve
1572 // in our stack for setting up arguments for calls to other
1573 // routines. If this optional field is not set, LE will reserve
1574 // 128 bytes for the argument area. This optional field is
1575 // created if greater than 128 bytes is required - to guarantee
1576 // the required space is reserved on stack extension in the new
1577 // extension. This optional field is also created if the
1578 // routine has alloca(). This may reduce stack space
1579 // if alloca() call causes a stack extension.
1580 bool HasArgAreaLength =
1581 (AllocaReg != 0) || (MFFrame.getMaxCallFrameSize() > 128);
1582
1583 bool HasName =
1584 MF->getFunction().hasName() && MF->getFunction().getName().size() > 0;
1585
1586 emitPPA1Flags(OutStreamer, MF->getFunction().isVarArg(),
1587 MFFrame.hasStackProtectorIndex(), SavedFPRMask != 0,
1588 TargetHasVector && SavedVRMask != 0, NeedEmitEHBlock,
1589 HasArgAreaLength, HasName);
1590
1591 OutStreamer->AddComment("Length/4 of Parms");
1592 OutStreamer->emitInt16(
1593 static_cast<uint16_t>(ZFI->getSizeOfFnParams() / 4)); // Parms/4.
1594 OutStreamer->AddComment("Length of Code");
1595 OutStreamer->emitAbsoluteSymbolDiff(FnEndSym, CurrentFnEPMarkerSym, 4);
1596
1597 if (HasArgAreaLength) {
1598 OutStreamer->AddComment("Argument Area Length");
1599 OutStreamer->emitInt32(MFFrame.getMaxCallFrameSize());
1600 }
1601
1602 // Emit saved FPR mask and offset to FPR save area (0x20 of flags 3).
1603 if (SavedFPRMask) {
1604 OutStreamer->AddComment("FPR mask");
1605 OutStreamer->emitInt16(SavedFPRMask);
1606 OutStreamer->AddComment("AR mask");
1607 OutStreamer->emitInt16(0); // AR Mask, unused currently.
1608 OutStreamer->AddComment("FPR Save Area Locator");
1609 OutStreamer->AddComment(Twine(" Bit 0-3: Register R")
1610 .concat(utostr(FrameAndFPROffset >> 28))
1611 .str());
1612 OutStreamer->AddComment(Twine(" Bit 4-31: Offset ")
1613 .concat(utostr(FrameAndFPROffset & 0x0FFFFFFF))
1614 .str());
1615 OutStreamer->emitInt32(FrameAndFPROffset); // Offset to FPR save area with
1616 // register to add value to
1617 // (alloca reg).
1618 }
1619
1620 // Emit saved VR mask to VR save area.
1621 if (TargetHasVector && SavedVRMask) {
1622 OutStreamer->AddComment("VR mask");
1623 OutStreamer->emitInt8(SavedVRMask);
1624 OutStreamer->emitInt8(0); // Reserved.
1625 OutStreamer->emitInt16(0); // Also reserved.
1626 OutStreamer->AddComment("VR Save Area Locator");
1627 OutStreamer->AddComment(Twine(" Bit 0-3: Register R")
1628 .concat(utostr(FrameAndVROffset >> 28))
1629 .str());
1630 OutStreamer->AddComment(Twine(" Bit 4-31: Offset ")
1631 .concat(utostr(FrameAndVROffset & 0x0FFFFFFF))
1632 .str());
1633 OutStreamer->emitInt32(FrameAndVROffset);
1634 }
1635
1636 // Emit C++ EH information block
1637 const Function *Per = nullptr;
1638 if (NeedEmitEHBlock) {
1639 Per = dyn_cast<Function>(
1640 MF->getFunction().getPersonalityFn()->stripPointerCasts());
1641 MCSymbol *PersonalityRoutine =
1642 Per ? MF->getTarget().getSymbol(Per) : nullptr;
1643 assert(PersonalityRoutine && "Missing personality routine");
1644
1645 OutStreamer->AddComment("Version");
1646 OutStreamer->emitInt32(1);
1647 OutStreamer->AddComment("Flags");
1648 OutStreamer->emitInt32(0); // LSDA field is a WAS offset
1649 OutStreamer->AddComment("Personality routine");
1650 OutStreamer->emitInt64(ADATable.insert(
1651 PersonalityRoutine, SystemZII::MO_ADA_INDIRECT_FUNC_DESC));
1652 OutStreamer->AddComment("LSDA location");
1653 MCSymbol *GCCEH = MF->getContext().getOrCreateSymbol(
1654 Twine("GCC_except_table") + Twine(MF->getFunctionNumber()));
1655 OutStreamer->emitInt64(
1656 ADATable.insert(GCCEH, SystemZII::MO_ADA_DATA_SYMBOL_ADDR));
1657 }
1658
1659 // Emit name length and name optional section (0x01 of flags 4)
1660 if (HasName)
1661 emitPPA1Name(OutStreamer, MF->getFunction().getName());
1662
1663 // Emit offset to entry point optional section (0x80 of flags 4).
1664 OutStreamer->emitAbsoluteSymbolDiff(CurrentFnEPMarkerSym, CurrentFnPPA1Sym,
1665 4);
1666}
1667
1669 if (TM.getTargetTriple().isOSzOS())
1670 emitPPA2(M);
1672}
1673
1674void SystemZAsmPrinter::emitPPA2(Module &M) {
1675 OutStreamer->pushSection();
1676 OutStreamer->switchSection(getObjFileLowering().getTextSection(),
1678 MCContext &OutContext = OutStreamer->getContext();
1679 // Make CELQSTRT symbol.
1680 const char *StartSymbolName = "CELQSTRT";
1681 MCSymbol *CELQSTRT = OutContext.getOrCreateSymbol(StartSymbolName);
1682 OutStreamer->emitSymbolAttribute(CELQSTRT, MCSA_OSLinkage);
1683 OutStreamer->emitSymbolAttribute(CELQSTRT, MCSA_Global);
1684
1685 // Create symbol and assign to class field for use in PPA1.
1686 PPA2Sym = OutContext.createTempSymbol("PPA2", false);
1687 MCSymbol *DateVersionSym = OutContext.createTempSymbol("DVS", false);
1688
1689 std::time_t Time = getTranslationTime(M);
1690 SmallString<14> CompilationTimeEBCDIC, CompilationTime;
1691 CompilationTime = formatv("{0:%Y%m%d%H%M%S}", llvm::sys::toUtcTime(Time));
1692
1693 uint32_t ProductVersion = getProductVersion(M),
1694 ProductRelease = getProductRelease(M),
1695 ProductPatch = getProductPatch(M);
1696
1697 SmallString<6> VersionEBCDIC, Version;
1698 Version = formatv("{0,0-2:d}{1,0-2:d}{2,0-2:d}", ProductVersion,
1699 ProductRelease, ProductPatch);
1700
1701 ConverterEBCDIC::convertToEBCDIC(CompilationTime, CompilationTimeEBCDIC);
1703
1704 enum class PPA2MemberId : uint8_t {
1705 // See z/OS Language Environment Vendor Interfaces v2r5, p.23, for
1706 // complete list. Only the C runtime is supported by this backend.
1707 LE_C_Runtime = 3,
1708 };
1709 enum class PPA2MemberSubId : uint8_t {
1710 // List of languages using the LE C runtime implementation.
1711 C = 0x00,
1712 CXX = 0x01,
1713 Swift = 0x03,
1714 Go = 0x60,
1715 LLVMBasedLang = 0xe7,
1716 };
1717 // PPA2 Flags
1718 enum class PPA2Flags : uint8_t {
1719 CompileForBinaryFloatingPoint = 0x80,
1720 CompiledWithXPLink = 0x01,
1721 CompiledUnitASCII = 0x04,
1722 HasServiceInfo = 0x20,
1723 };
1724
1725 PPA2MemberSubId MemberSubId = PPA2MemberSubId::LLVMBasedLang;
1726 if (auto *MD = M.getModuleFlag("zos_cu_language")) {
1727 StringRef Language = cast<MDString>(MD)->getString();
1728 MemberSubId = StringSwitch<PPA2MemberSubId>(Language)
1729 .Case("C", PPA2MemberSubId::C)
1730 .Case("C++", PPA2MemberSubId::CXX)
1731 .Case("Swift", PPA2MemberSubId::Swift)
1732 .Case("Go", PPA2MemberSubId::Go)
1733 .Default(PPA2MemberSubId::LLVMBasedLang);
1734 }
1735
1736 // Emit PPA2 section.
1737 OutStreamer->emitLabel(PPA2Sym);
1738 OutStreamer->emitInt8(static_cast<uint8_t>(PPA2MemberId::LE_C_Runtime));
1739 OutStreamer->emitInt8(static_cast<uint8_t>(MemberSubId));
1740 OutStreamer->emitInt8(0x22); // Member defined, c370_plist+c370_env
1741 OutStreamer->emitInt8(0x04); // Control level 4 (XPLink)
1742 OutStreamer->emitAbsoluteSymbolDiff(CELQSTRT, PPA2Sym, 4);
1743 OutStreamer->emitInt32(0x00000000);
1744 OutStreamer->emitAbsoluteSymbolDiff(DateVersionSym, PPA2Sym, 4);
1745 OutStreamer->emitInt32(
1746 0x00000000); // Offset to main entry point, always 0 (so says TR).
1747 uint8_t Flgs = static_cast<uint8_t>(PPA2Flags::CompileForBinaryFloatingPoint);
1748 Flgs |= static_cast<uint8_t>(PPA2Flags::CompiledWithXPLink);
1749
1750 bool IsASCII = true;
1751 if (auto *MD = M.getModuleFlag("zos_le_char_mode")) {
1752 const StringRef &CharMode = cast<MDString>(MD)->getString();
1753 if (CharMode == "ebcdic")
1754 IsASCII = false;
1755 else if (CharMode != "ascii")
1756 OutContext.reportError(
1757 {}, "Only ascii or ebcdic are allowed for zos_le_char_mode");
1758 }
1759 if (IsASCII)
1760 Flgs |= static_cast<uint8_t>(
1761 PPA2Flags::CompiledUnitASCII); // Setting bit for ASCII char. mode.
1762
1763 OutStreamer->emitInt8(Flgs);
1764 OutStreamer->emitInt8(0x00); // Reserved.
1765 // No MD5 signature before timestamp.
1766 // No FLOAT(AFP(VOLATILE)).
1767 // Remaining 5 flag bits reserved.
1768 OutStreamer->emitInt16(0x0000); // 16 Reserved flag bits.
1769
1770 // Emit date and version section.
1771 OutStreamer->emitLabel(DateVersionSym);
1772 OutStreamer->emitBytes(CompilationTimeEBCDIC.str());
1773 OutStreamer->emitBytes(VersionEBCDIC.str());
1774
1775 OutStreamer->emitInt16(0x0000); // Service level string length.
1776
1777 // The binder requires that the offset to the PPA2 be emitted in a different,
1778 // specially-named section.
1779 OutStreamer->switchSection(getObjFileLowering().getPPA2ListSection());
1780 // Emit 8 byte alignment.
1781 // Emit pointer to PPA2 label.
1782 OutStreamer->AddComment("A(PPA2-CELQSTRT)");
1783 OutStreamer->emitAbsoluteSymbolDiff(PPA2Sym, CELQSTRT, 8);
1784 OutStreamer->popSection();
1785}
1786
1788 const SystemZSubtarget &Subtarget = MF->getSubtarget<SystemZSubtarget>();
1789
1790 if (Subtarget.getTargetTriple().isOSzOS()) {
1791 MCContext &OutContext = OutStreamer->getContext();
1792
1793 // Save information for later use.
1794 std::string N(MF->getFunction().hasName()
1795 ? Twine(MF->getFunction().getName()).concat("_").str()
1796 : "");
1797
1798 CurrentFnEPMarkerSym =
1799 OutContext.createTempSymbol(Twine("EPM_").concat(N).str(), true);
1800 CurrentFnPPA1Sym =
1801 OutContext.createTempSymbol(Twine("PPA1_").concat(N).str(), true);
1802
1803 // EntryPoint Marker
1804 const MachineFrameInfo &MFFrame = MF->getFrameInfo();
1805 bool IsUsingAlloca = MFFrame.hasVarSizedObjects();
1806 uint32_t DSASize = MFFrame.getStackSize();
1807 bool IsLeaf = DSASize == 0 && MFFrame.getCalleeSavedInfo().empty();
1808
1809 // Set Flags.
1810 uint8_t Flags = 0;
1811 if (IsLeaf)
1812 Flags |= 0x08;
1813 if (IsUsingAlloca)
1814 Flags |= 0x04;
1815
1816 // Combine into top 27 bits of DSASize and bottom 5 bits of Flags.
1817 uint32_t DSAAndFlags = DSASize & 0xFFFFFFE0; // (x/32) << 5
1818 DSAAndFlags |= Flags;
1819
1820 // Emit entry point marker section.
1821 OutStreamer->AddComment("XPLINK Routine Layout Entry");
1822 OutStreamer->emitLabel(CurrentFnEPMarkerSym);
1823 OutStreamer->AddComment("Eyecatcher 0x00C300C500C500");
1824 OutStreamer->emitIntValueInHex(0x00C300C500C500, 7); // Eyecatcher.
1825 OutStreamer->AddComment("Mark Type C'1'");
1826 OutStreamer->emitInt8(0xF1); // Mark Type.
1827 OutStreamer->AddComment("Offset to PPA1");
1828 OutStreamer->emitAbsoluteSymbolDiff(CurrentFnPPA1Sym, CurrentFnEPMarkerSym,
1829 4);
1830 if (OutStreamer->isVerboseAsm()) {
1831 OutStreamer->AddComment("DSA Size 0x" + Twine::utohexstr(DSASize));
1832 OutStreamer->AddComment("Entry Flags");
1833 if (Flags & 0x08)
1834 OutStreamer->AddComment(" Bit 1: 1 = Leaf function");
1835 else
1836 OutStreamer->AddComment(" Bit 1: 0 = Non-leaf function");
1837 if (Flags & 0x04)
1838 OutStreamer->AddComment(" Bit 2: 1 = Uses alloca");
1839 else
1840 OutStreamer->AddComment(" Bit 2: 0 = Does not use alloca");
1841 }
1842 OutStreamer->emitInt32(DSAAndFlags);
1843 }
1844
1846}
1847
1848char SystemZAsmPrinter::ID = 0;
1849
1850INITIALIZE_PASS(SystemZAsmPrinter, "systemz-asm-printer",
1851 "SystemZ Assembly Printer", false, false)
1852
1853// Force static initialization.
1854extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void
1855LLVMInitializeSystemZAsmPrinter() {
1857}
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,...