Jim Stichnoth | a5b16ab | 2016-05-10 11:20:41 -0700 | [diff] [blame] | 1 | //===- NaClBitcodeDecoders.cpp --------------------------------------------===// |
| 2 | // Internal implementation of decoder functions for PNaCl Bitcode files. |
| 3 | // |
| 4 | // The LLVM Compiler Infrastructure |
| 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See LICENSE.TXT for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #include "llvm/Bitcode/NaCl/NaClBitcodeDecoders.h" |
| 12 | |
| 13 | namespace llvm { |
| 14 | namespace naclbitc { |
| 15 | |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame^] | 16 | bool DecodeCastOpcode(uint64_t NaClOpcode, Instruction::CastOps &LLVMOpcode) { |
Jim Stichnoth | a5b16ab | 2016-05-10 11:20:41 -0700 | [diff] [blame] | 17 | switch (NaClOpcode) { |
| 18 | default: |
| 19 | LLVMOpcode = Instruction::BitCast; |
| 20 | return false; |
| 21 | case naclbitc::CAST_TRUNC: |
| 22 | LLVMOpcode = Instruction::Trunc; |
| 23 | return true; |
| 24 | case naclbitc::CAST_ZEXT: |
| 25 | LLVMOpcode = Instruction::ZExt; |
| 26 | return true; |
| 27 | case naclbitc::CAST_SEXT: |
| 28 | LLVMOpcode = Instruction::SExt; |
| 29 | return true; |
| 30 | case naclbitc::CAST_FPTOUI: |
| 31 | LLVMOpcode = Instruction::FPToUI; |
| 32 | return true; |
| 33 | case naclbitc::CAST_FPTOSI: |
| 34 | LLVMOpcode = Instruction::FPToSI; |
| 35 | return true; |
| 36 | case naclbitc::CAST_UITOFP: |
| 37 | LLVMOpcode = Instruction::UIToFP; |
| 38 | return true; |
| 39 | case naclbitc::CAST_SITOFP: |
| 40 | LLVMOpcode = Instruction::SIToFP; |
| 41 | return true; |
| 42 | case naclbitc::CAST_FPTRUNC: |
| 43 | LLVMOpcode = Instruction::FPTrunc; |
| 44 | return true; |
| 45 | case naclbitc::CAST_FPEXT: |
| 46 | LLVMOpcode = Instruction::FPExt; |
| 47 | return true; |
| 48 | case naclbitc::CAST_BITCAST: |
| 49 | LLVMOpcode = Instruction::BitCast; |
| 50 | return true; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | bool DecodeLinkage(uint64_t NaClLinkage, |
| 55 | GlobalValue::LinkageTypes &LLVMLinkage) { |
| 56 | switch (NaClLinkage) { |
| 57 | default: |
| 58 | LLVMLinkage = GlobalValue::InternalLinkage; |
| 59 | return false; |
| 60 | case naclbitc::LINKAGE_EXTERNAL: |
| 61 | LLVMLinkage = GlobalValue::ExternalLinkage; |
| 62 | return true; |
| 63 | case naclbitc::LINKAGE_INTERNAL: |
| 64 | LLVMLinkage = GlobalValue::InternalLinkage; |
| 65 | return true; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | bool DecodeBinaryOpcode(uint64_t NaClOpcode, Type *Ty, |
| 70 | Instruction::BinaryOps &LLVMOpcode) { |
| 71 | switch (NaClOpcode) { |
| 72 | default: |
| 73 | LLVMOpcode = Instruction::Add; |
| 74 | return false; |
| 75 | case naclbitc::BINOP_ADD: |
| 76 | LLVMOpcode = Ty->isFPOrFPVectorTy() ? Instruction::FAdd : Instruction::Add; |
| 77 | return true; |
| 78 | case naclbitc::BINOP_SUB: |
| 79 | LLVMOpcode = Ty->isFPOrFPVectorTy() ? Instruction::FSub : Instruction::Sub; |
| 80 | return true; |
| 81 | case naclbitc::BINOP_MUL: |
| 82 | LLVMOpcode = Ty->isFPOrFPVectorTy() ? Instruction::FMul : Instruction::Mul; |
| 83 | return true; |
| 84 | case naclbitc::BINOP_UDIV: |
| 85 | LLVMOpcode = Instruction::UDiv; |
| 86 | return true; |
| 87 | case naclbitc::BINOP_SDIV: |
| 88 | LLVMOpcode = Ty->isFPOrFPVectorTy() ? Instruction::FDiv : Instruction::SDiv; |
| 89 | return true; |
| 90 | case naclbitc::BINOP_UREM: |
| 91 | LLVMOpcode = Instruction::URem; |
| 92 | return true; |
| 93 | case naclbitc::BINOP_SREM: |
| 94 | LLVMOpcode = Ty->isFPOrFPVectorTy() ? Instruction::FRem : Instruction::SRem; |
| 95 | return true; |
| 96 | case naclbitc::BINOP_SHL: |
| 97 | LLVMOpcode = Instruction::Shl; |
| 98 | return true; |
| 99 | case naclbitc::BINOP_LSHR: |
| 100 | LLVMOpcode = Instruction::LShr; |
| 101 | return true; |
| 102 | case naclbitc::BINOP_ASHR: |
| 103 | LLVMOpcode = Instruction::AShr; |
| 104 | return true; |
| 105 | case naclbitc::BINOP_AND: |
| 106 | LLVMOpcode = Instruction::And; |
| 107 | return true; |
| 108 | case naclbitc::BINOP_OR: |
| 109 | LLVMOpcode = Instruction::Or; |
| 110 | return true; |
| 111 | case naclbitc::BINOP_XOR: |
| 112 | LLVMOpcode = Instruction::Xor; |
| 113 | return true; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | bool DecodeCallingConv(uint64_t NaClCallingConv, |
| 118 | CallingConv::ID &LLVMCallingConv) { |
| 119 | switch (NaClCallingConv) { |
| 120 | default: |
| 121 | LLVMCallingConv = CallingConv::C; |
| 122 | return false; |
| 123 | case naclbitc::C_CallingConv: |
| 124 | LLVMCallingConv = CallingConv::C; |
| 125 | return true; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | bool DecodeFcmpPredicate(uint64_t NaClPredicate, |
| 130 | CmpInst::Predicate &LLVMPredicate) { |
| 131 | switch (NaClPredicate) { |
| 132 | default: |
| 133 | LLVMPredicate = CmpInst::FCMP_FALSE; |
| 134 | return false; |
| 135 | case naclbitc::FCMP_FALSE: |
| 136 | LLVMPredicate = CmpInst::FCMP_FALSE; |
| 137 | return true; |
| 138 | case naclbitc::FCMP_OEQ: |
| 139 | LLVMPredicate = CmpInst::FCMP_OEQ; |
| 140 | return true; |
| 141 | case naclbitc::FCMP_OGT: |
| 142 | LLVMPredicate = CmpInst::FCMP_OGT; |
| 143 | return true; |
| 144 | case naclbitc::FCMP_OGE: |
| 145 | LLVMPredicate = CmpInst::FCMP_OGE; |
| 146 | return true; |
| 147 | case naclbitc::FCMP_OLT: |
| 148 | LLVMPredicate = CmpInst::FCMP_OLT; |
| 149 | return true; |
| 150 | case naclbitc::FCMP_OLE: |
| 151 | LLVMPredicate = CmpInst::FCMP_OLE; |
| 152 | return true; |
| 153 | case naclbitc::FCMP_ONE: |
| 154 | LLVMPredicate = CmpInst::FCMP_ONE; |
| 155 | return true; |
| 156 | case naclbitc::FCMP_ORD: |
| 157 | LLVMPredicate = CmpInst::FCMP_ORD; |
| 158 | return true; |
| 159 | case naclbitc::FCMP_UNO: |
| 160 | LLVMPredicate = CmpInst::FCMP_UNO; |
| 161 | return true; |
| 162 | case naclbitc::FCMP_UEQ: |
| 163 | LLVMPredicate = CmpInst::FCMP_UEQ; |
| 164 | return true; |
| 165 | case naclbitc::FCMP_UGT: |
| 166 | LLVMPredicate = CmpInst::FCMP_UGT; |
| 167 | return true; |
| 168 | case naclbitc::FCMP_UGE: |
| 169 | LLVMPredicate = CmpInst::FCMP_UGE; |
| 170 | return true; |
| 171 | case naclbitc::FCMP_ULT: |
| 172 | LLVMPredicate = CmpInst::FCMP_ULT; |
| 173 | return true; |
| 174 | case naclbitc::FCMP_ULE: |
| 175 | LLVMPredicate = CmpInst::FCMP_ULE; |
| 176 | return true; |
| 177 | case naclbitc::FCMP_UNE: |
| 178 | LLVMPredicate = CmpInst::FCMP_UNE; |
| 179 | return true; |
| 180 | case naclbitc::FCMP_TRUE: |
| 181 | LLVMPredicate = CmpInst::FCMP_TRUE; |
| 182 | return true; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | bool DecodeIcmpPredicate(uint64_t NaClPredicate, |
| 187 | CmpInst::Predicate &LLVMPredicate) { |
| 188 | switch (NaClPredicate) { |
| 189 | default: |
| 190 | LLVMPredicate = CmpInst::ICMP_EQ; |
| 191 | return false; |
| 192 | case naclbitc::ICMP_EQ: |
| 193 | LLVMPredicate = CmpInst::ICMP_EQ; |
| 194 | return true; |
| 195 | case naclbitc::ICMP_NE: |
| 196 | LLVMPredicate = CmpInst::ICMP_NE; |
| 197 | return true; |
| 198 | case naclbitc::ICMP_UGT: |
| 199 | LLVMPredicate = CmpInst::ICMP_UGT; |
| 200 | return true; |
| 201 | case naclbitc::ICMP_UGE: |
| 202 | LLVMPredicate = CmpInst::ICMP_UGE; |
| 203 | return true; |
| 204 | case naclbitc::ICMP_ULT: |
| 205 | LLVMPredicate = CmpInst::ICMP_ULT; |
| 206 | return true; |
| 207 | case naclbitc::ICMP_ULE: |
| 208 | LLVMPredicate = CmpInst::ICMP_ULE; |
| 209 | return true; |
| 210 | case naclbitc::ICMP_SGT: |
| 211 | LLVMPredicate = CmpInst::ICMP_SGT; |
| 212 | return true; |
| 213 | case naclbitc::ICMP_SGE: |
| 214 | LLVMPredicate = CmpInst::ICMP_SGE; |
| 215 | return true; |
| 216 | case naclbitc::ICMP_SLT: |
| 217 | LLVMPredicate = CmpInst::ICMP_SLT; |
| 218 | return true; |
| 219 | case naclbitc::ICMP_SLE: |
| 220 | LLVMPredicate = CmpInst::ICMP_SLE; |
| 221 | return true; |
| 222 | } |
| 223 | } |
| 224 | |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame^] | 225 | } // namespace naclbitc |
| 226 | } // namespace llvm |