| 1 | //===- AMDGPUDelayedMCExpr.cpp - Delayed MCExpr resolve ---------*- C++ -*-===// |
| 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 | #include "AMDGPUDelayedMCExpr.h" |
| 10 | #include "llvm/MC/MCExpr.h" |
| 11 | #include "llvm/MC/MCValue.h" |
| 12 | |
| 13 | using namespace llvm; |
| 14 | |
| 15 | static msgpack::DocNode getNode(msgpack::DocNode DN, msgpack::Type Type, |
| 16 | MCValue Val) { |
| 17 | msgpack::Document *Doc = DN.getDocument(); |
| 18 | switch (Type) { |
| 19 | default: |
| 20 | return Doc->getEmptyNode(); |
| 21 | case msgpack::Type::Int: |
| 22 | return Doc->getNode(V: static_cast<int64_t>(Val.getConstant())); |
| 23 | case msgpack::Type::UInt: |
| 24 | return Doc->getNode(V: static_cast<uint64_t>(Val.getConstant())); |
| 25 | case msgpack::Type::Boolean: |
| 26 | return Doc->getNode(V: static_cast<bool>(Val.getConstant())); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | void DelayedMCExprs::assignDocNode(msgpack::DocNode &DN, msgpack::Type Type, |
| 31 | const MCExpr *ExprValue) { |
| 32 | MCValue Res; |
| 33 | if (ExprValue->evaluateAsRelocatable(Res, Asm: nullptr)) { |
| 34 | if (Res.isAbsolute()) { |
| 35 | DN = getNode(DN, Type, Val: Res); |
| 36 | return; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | DelayedExprs.emplace_back(args&: DN, args&: Type, args&: ExprValue); |
| 41 | } |
| 42 | |
| 43 | bool DelayedMCExprs::resolveDelayedExpressions() { |
| 44 | while (!DelayedExprs.empty()) { |
| 45 | Expr DE = DelayedExprs.front(); |
| 46 | MCValue Res; |
| 47 | |
| 48 | if (!DE.ExprValue->evaluateAsRelocatable(Res, Asm: nullptr) || !Res.isAbsolute()) |
| 49 | return false; |
| 50 | |
| 51 | DelayedExprs.pop_front(); |
| 52 | DE.DN = getNode(DN: DE.DN, Type: DE.Type, Val: Res); |
| 53 | } |
| 54 | |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | void DelayedMCExprs::clear() { DelayedExprs.clear(); } |
| 59 | |
| 60 | bool DelayedMCExprs::empty() { return DelayedExprs.empty(); } |
| 61 | |